Load Picturebox Image From Memory?
Asked Answered
K

3

12

I can't seem to figure out how to load a pictureBox image from a bitmap in memory. Is it possible or do I have to create temp file for the bitmap?

Kucera answered 29/3, 2010 at 19:42 Comment(3)
Possible duplicate: stackoverflow.com/questions/743549Hesiod
How do you have the bitmap in memory?Cestus
It is the opposite, loading the image from a file, that is unusual. Use the Bitmap class.Eros
Z
24

What format is the image in memory?

If you have an actual Bitmap object, just assign it to the PictureBox, as suggested by dtb:

pictureBox.Image = bitmap;

If you have the image as a series of bytes held in a stream, you'll need to load the image from the stream:

var image = Image.FromStream(stream);
pictureBox.Image = image;

If you instead have a windows GDI handle to the bitmap, use

var image = Image.FromHbitmap(handle);
pictureBox.Image = image;

Essentially, it's hard to answer your question with more than suggestions when you haven't told us what format the Bitmap you have is held in.

Zacharyzacherie answered 29/3, 2010 at 19:49 Comment(2)
I have received System.ArgumentException: Parameter is not valid. exception when i tried to load image from stream.Ireland
@BalagurunathanMarimuthu I suggest posting your own question to get assistance with your particular context; it's difficult to assist with so little information.Zacharyzacherie
C
4

You can create a Bitmap from a MemoryStream:

pictureBox.Image = new Bitmap(new MemoryStream(byteArray));
Cestus answered 29/3, 2010 at 19:48 Comment(3)
at what point is it safe to explicitly Dispose of the MemoryStream ? Would : using(var ms = new MemorySteam(byteArray)){ pictureBox.Image = new Bitmap(ms) }; be safe ?Agenesis
@MoeSisko: You don't really need to dispose a MemoryStream; they don't have unmanaged resoruces. (just a byte[]) But, yes; that should be fine.Cestus
parameter missing here Dim picture As Byte() = GetBytes(ListView2.Items(index).SubItems(8).Text) Dim converter As New ImageConverter() PictureBox1.Image = DirectCast(converter.ConvertFrom(picture), Image)Unchain
H
2
pictureBox.Image = bitmap;
Hesiod answered 29/3, 2010 at 19:44 Comment(1)
parameter missing here Dim picture As Byte() = GetBytes(ListView2.Items(index).SubItems(8).Text) Dim converter As New ImageConverter() PictureBox1.Image = DirectCast(converter.ConvertFrom(picture), Image)Unchain

© 2022 - 2024 — McMap. All rights reserved.