2010-08-05 42 views

回答

6

您可以從磁盤讀取圖像的字節到一個字節數組,然後創建您的BitmapImage對象。

var stream = new MemoryStream(imageBytes); 
var img = new System.Windows.Media.Imaging.BitmapImage(); 

img.BeginInit(); 
img.StreamSource = stream; 
img.EndInit(); 

return img; 
+3

但這種泄漏的MemoryStream!您需要設置CacheOption = OnLoad並在之後處置流。 – Vlad 2015-08-28 12:08:05

2

的代碼如下:

FileStream fileStream = 
    new FileStream(fileName, FileMode.Open, FileAccess.Read); 

var img = new System.Windows.Media.Imaging.BitmapImage(); 
img.BeginInit(); 
img.StreamSource = fileStream; 
img.EndInit(); 
31

這個工作對我來說:

BitmapSource bSource = new BitmapImage(new Uri("c:\\image.bmp")); 
BitmapImage bImage = new BitmapImage(new Uri("c:\\image.bmp"));