如何从图像文件中加载BitmapSource
?
答案 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();