来自文件的BitmapSource

时间:2010-08-05 19:16:07

标签: c# wpf

如何从图像文件中加载BitmapSource

3 个答案:

答案 0 :(得分:40)

这对我有用:

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

答案 1 :(得分:7)

您可以将图像的字节从磁盘读入字节数组,然后创建BitmapImage对象。

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

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

return img;

答案 2 :(得分: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();