Подтвердить что ты не робот

BitmapSource из файла

Как я могу загрузить BitmapSource из файла изображения?

4b9b3361

Ответ 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();