Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IO.Stream to Image in WPF

Tags:

c#

wpf

I am trying to read an image from a resource only DLL file. I am able to read the image name and image bytes, but How do I set the Image control to stream buffer? In windows form, I know I can use this :

pictureBox1.Image=new System.Drawing.Bitmap(IOStream);

since there is no Drawing namespace in wpf, how can I achieve the same thing?

like image 885
Tanuj Wadhwa Avatar asked May 31 '13 12:05

Tanuj Wadhwa


2 Answers

In WPF, you can set the Source property of an Image, as in this example:

Image image = new Image();
using (MemoryStream stream = new MemoryStream(byteArray))
{
    image.Source = BitmapFrame.Create(stream,
                                      BitmapCreateOptions.None,
                                      BitmapCacheOption.OnLoad);
}

Where byteArray is the array of bytes with the source of the image.

like image 64
John Willemse Avatar answered Nov 15 '22 20:11

John Willemse


In WPF you probably have an Image element in your xaml. The Source can be any BitmapImage. You can bind a BitmapImage from your ViewModel, where you can create an instance from a Stream like this.

like image 33
nvoigt Avatar answered Nov 15 '22 21:11

nvoigt