Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a byte[] into an Image at Runtime

Tags:

c#

image

wpf

I have a byte[] that is represented by an Image. I am downloading this Image via a WebClient. When the WebClient has downloaded the picture and I reference it using its 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: Generate Image at Runtime. I cannot seem to get that approach to work, so I am trying a different approach.

like image 607
user70192 Avatar asked Feb 24 '09 17:02

user70192


People also ask

How do I save a byte array as a picture?

Create a ByteArrayInputStream object by passing the byte array (that is to be converted) to its constructor. Read the image using the read() method of the ImageIO class (by passing the ByteArrayInputStream objects to it as a parameter). Finally, Write the image to using the write() method of the ImageIo class.

What is byte array image?

Images are binary data - this is easily represented as byte arrays. The image in the sample is stored in the database as a BLOB - not a string or location, that is, it is binary data.

What is C# byte array?

With byte arrays, we can store binary data. This data may be part of a data file, image file, compressed file or downloaded server response. Array info. With byte arrays, we have an ideal representation of this data. The byte array type allows us to store low-level representations.


1 Answers

Create a BitmapImage from the MemoryStream as below:

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

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

like image 99
Jobi Joy Avatar answered Oct 23 '22 04:10

Jobi Joy