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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With