tags:

views:

1121

answers:

2

I have a byte[] that represents by Image. I am downloading this Image via a WebClient. When the WebClient has downloaded the picture I reference in via a URL, I get a byte[]. My question is, how do I load a byte[] into an Image element in WPF? Thank you.

Note: This is complementary to the question I asked here: http://stackoverflow.com/questions/580359/generate-image-at-runtime. I cannot seem to get that approach to work, so I am trying a different approach.

+5  A: 

Create a BitmapImage from the memory stream as bellow

 MemoryStream byteStream = new MemoryStream( bytes );
 BitmapImage image = new BitmapImage();
 image.BeginInit();
 image.StreamSource = byteStream ;
 image.EndInit();

And in the XAML you can create a Image control and set the above 'image' as the Source property

Jobi Joy
Wouldn't that only work for WinForms?
configurator
Updated the answer
Jobi Joy
+2  A: 

You can use a BitmapImage, and sets its StreamSource to a stream containing the binary data. If you want to make a stream from a byte[], use a MemoryStream:

MemoryStream stream = new MemoryStream(bytes);
configurator