2013-12-14 118 views
-1

有沒有一種方法可以從內存中的base64字符串或字節數組中顯示圖像,如gif,png,jpg,tiff,而無需將它們寫入磁盤?WPF在不寫入文件的情況下加載圖像

+0

([從MemoryStream的PNG,GIF創建WPF的BitmapImage]的可能重複http://stackoverflow.com/questions/2097152/creating-wpf-bitmapimage-from-memorystream -png-gif) –

+0

你可以讓[ImageSourceConverter](http://msdn.microsoft.com/en-us/library/system.windows.media.imagesourceconverter.aspx)完成這項工作。它支持或多或少所有可能的ImageSource轉換,包括來自包含編碼圖像緩衝區的字節數組的轉換。 – Clemens

回答

2

沒有要求圖像源是本地文件的路徑。即您可以從字節數組中創建BitmapSource(直接或首先從流中讀取所有字節)並使用它。

BitmapSource

MSDN樣品:

// Define parameters used to create the BitmapSource. 
PixelFormat pf = PixelFormats.Bgr32; 
int width = 200; 
int height = 200; 
int rawStride = (width * pf.BitsPerPixel + 7)/8; 

// Create a BitmapSource. 
BitmapSource bitmap = BitmapSource.Create(width, height, 
    96, 96, pf, null, 
    new byte[rawStride * height], rawStride); 

// Create an image element; 
Image myImage = new Image() { Width = 200 }; 
// Set image source. 
myImage.Source = bitmap; 
+0

感謝這有所幫助。得到我需要的。 – Tsukasa

相關問題