2013-03-07 58 views
12

我有一個代表圖像原始數據的byte[]。我想將其轉換爲BitmapImage如何將字節[]轉換爲BitmapImage?

我試了幾個例子,我發現,但我一直得到以下異常

「適合來完成這個操作沒有成像部件被發現。」

我認爲這是因爲我的byte[]實際上並不代表一個圖像,而只是原始位。 所以我的問題是如上所述是如何將原始位的字節[]轉換爲BitmapImage

回答

8

當你的字節數組包含一個位圖的原始像素數據,您可以創建一個BitmapSource(這是基類的BitmapImage)的靜態方法BitmapSource.Create

但是,您需要指定位圖的幾個參數。您必須事先知道緩衝區的寬度和高度以及PixelFormat

byte[] buffer = ...; 

var width = 100; // for example 
var height = 100; // for example 
var dpiX = 96d; 
var dpiY = 96d; 
var pixelFormat = PixelFormats.Pbgra32; // for example 
var bytesPerPixel = (pixelFormat.BitsPerPixel + 7)/8; 
var stride = bytesPerPixel * width; 

var bitmap = BitmapSource.Create(width, height, dpiX, dpiY, 
           pixelFormat, null, buffer, stride); 
+0

在Windows Phone 8上,以上不起作用,因爲BitmapSource不支持創建選項。你會知道任何替代品嗎? – vishal 2013-07-18 04:09:29

+1

@vishal你將不得不使用[WriteableBitmap](http://msdn.microsoft。COM/EN-US /庫/窗/應用/ windows.ui.xaml.media.imaging.writeablebitmap.aspx)。 – Clemens 2013-07-18 05:30:03

+0

謝謝。我已經在嘗試這個選項了:http://stackoverflow.com/questions/17714662/convert-int-to-byte-type-pointer-in-c-sharp/17715106?noredirect=1#17715106 – vishal 2013-07-18 05:31:04

-1

從原始字節創建一個MemoryStream並將其傳遞到您的位圖構造函數中。

像這樣:

MemoryStream stream = new  MemoryStream(bytes); 
Bitmap image = new Bitmap(stream); 
+0

你指的是'System.Drawing.Bitmap',它不是WPF。 – Clemens 2013-03-07 13:17:03

11
public static BitmapImage LoadFromBytes(byte[] bytes) 
{ 
    using (var stream = new MemoryStream(bytes)) 
    { 
     stream.Seek(0, SeekOrigin.Begin); 
     var image = new BitmapImage(); 
     image.BeginInit(); 
     image.StreamSource = stream; 
     image.EndInit(); 

     return image; 
    } 
} 
+0

其實,我試過這個,但是直到我在這個問題上添加了來自OP的代碼行後才起作用:http://stackoverflow.com/questions/35831893/bitmapimage-to-byte-and-reverse:'image。 CreateOptions = BitmapCreateOptions.PreservePixelFormat; image.CacheOption = BitmapCacheOption.OnLoad;' - 否則我只是有一個空白的圖像區域。 – vapcguy 2016-10-12 00:54:04

0

我跑過這個相同的錯誤,但這是因爲我的數組沒有得到充實的實際數據。我有一個字節數組,它等於它應該是的長度,但值仍然是0 - 他們還沒有被寫入!

我的特別問題 - 我懷疑其他人也會遇到這個問題 - 這是因爲OracleBlob參數。我不認爲我需要它,並認爲我可以做這樣的事情:

DataSet ds = new DataSet(); 
OracleCommand cmd = new OracleCommand(strQuery, conn); 
OracleDataAdapter oraAdpt = new OracleDataAdapter(cmd); 
oraAdpt.Fill(ds) 

if (ds.Tables[0].Rows.Count > 0) 
{ 
    byte[] myArray = (bytes)ds.Tables[0]["MY_BLOB_COLUMN"]; 
} 

我是多麼的錯!要真正獲得該blob中的實際字節,我需要將該結果實際讀入OracleBlob對象。相反,填充數據集/ DataTable的,我這樣做:

OracleBlob oBlob = null; 
byte[] myArray = null; 
OracleCommand cmd = new OracleCommand(strQuery, conn); 
OracleDataReader result = cmd.ExecuteReader(); 
result.Read(); 

if (result.HasRows) 
{ 
    oBlob = result.GetOracleBlob(0); 
    myArray = new byte[oBlob.Length]; 
    oBlob.Read(array, 0, Convert.ToInt32(myArray.Length)); 
    oBlob.Erase(); 
    oBlob.Close(); 
    oBlob.Dispose(); 
} 

然後,我可以採取myArray和做到這一點:

if (myArray != null) 
{ 
    if (myArray.Length > 0) 
    { 
     MyImage.Source = LoadBitmapFromBytes(myArray); 
    } 
} 

而且我從對方的回答修訂LoadBitmapFromBytes功能:

public static BitmapImage LoadBitmapFromBytes(byte[] bytes) 
{ 
    var image = new BitmapImage(); 
    using (var stream = new MemoryStream(bytes)) 
    { 
     stream.Seek(0, SeekOrigin.Begin); 
     image.BeginInit(); 
     image.StreamSource = stream; 
     image.CreateOptions = BitmapCreateOptions.PreservePixelFormat; 
     image.CacheOption = BitmapCacheOption.OnLoad; 
     image.UriSource = null; 
     image.EndInit(); 
    } 

    return image; 
}