Как я могу загрузить BitmapSource
из файла изображения?
BitmapSource из файла
Ответ 1
Это работает для меня:
BitmapSource bSource = new BitmapImage(new Uri("c:\\image.bmp"));
BitmapImage bImage = new BitmapImage(new Uri("c:\\image.bmp"));
Ответ 2
Вы можете прочитать байты изображения с диска в массив байтов, а затем создать свой объект BitmapImage.
var stream = new MemoryStream(imageBytes);
var img = new System.Windows.Media.Imaging.BitmapImage();
img.BeginInit();
img.StreamSource = stream;
img.EndInit();
return img;
Ответ 3
Следующий код:
FileStream fileStream =
new FileStream(fileName, FileMode.Open, FileAccess.Read);
var img = new System.Windows.Media.Imaging.BitmapImage();
img.BeginInit();
img.StreamSource = fileStream;
img.EndInit();