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?

cam
  • 8,725
  • 18
  • 57
  • 81

3 Answers3

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.

Bevan
  • 43,618
  • 10
  • 81
  • 133
  • I have received `System.ArgumentException: Parameter is not valid.` exception when i tried to load image from stream. – Balagurunathan Marimuthu Mar 04 '17 at 12:13
  • @BalagurunathanMarimuthu I suggest posting your own question to get assistance with your particular context; it's difficult to assist with so little information. – Bevan Mar 07 '17 at 18:07
4

You can create a Bitmap from a MemoryStream:

pictureBox.Image = new Bitmap(new MemoryStream(byteArray));
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 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 ? – Moe Sisko Oct 15 '12 at 23:20
  • @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. – SLaks Oct 15 '12 at 23:21
  • 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)` – nouman arshad Dec 27 '15 at 21:51
2
pictureBox.Image = bitmap;
dtb
  • 213,145
  • 36
  • 401
  • 431
  • 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)` – nouman arshad Dec 27 '15 at 21:51