18

I've a byte array which contains an image binary data in bitmap format. How do I display it using the PictureBox control in C#?

I went thru a couple of posts listed below but not sure if I need to convert the byte array into something else before sending it to a picturebox. I'd appreciate your help. Thanks!

How to put image in a picture box from Bitmap Load Picturebox Image From Memory?

Community
  • 1
  • 1
Kevin
  • 515
  • 4
  • 11
  • 22

7 Answers7

54

This function converts byte array into Bitmap which can be use to set the Image Property of the picturebox.

public static Bitmap ByteToImage(byte[] blob)
{
    MemoryStream mStream = new MemoryStream();
    byte[] pData = blob;
    mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
    Bitmap bm = new Bitmap(mStream, false);
    mStream.Dispose();
    return bm;
}

Sample usage:

pictureBox.Image = ByteToImage(byteArr); // byteArr holds byte array value
Jared Lovin
  • 543
  • 9
  • 24
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • 1
    What's the purpose of the `pData` variable? There was [a suggested edit](http://stackoverflow.com/review/suggested-edits/10017849) to remove it and just use blob directly. – cgmb Oct 29 '15 at 01:46
  • 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)` – nouman arshad Dec 27 '15 at 21:50
  • 6
    `Dim bm As New Bitmap(mStream, False)` parameter not valid – office 302 Dec 28 '15 at 10:06
6
byte[] imageSource = **byte array**;
Bitmap image;
using (MemoryStream stream = new MemoryStream(imageSource))
{
   image = new Bitmap(stream);
}
pictureBox.Image = image;
Wizetux
  • 756
  • 3
  • 12
  • 1
    I think this is a incorrect answer. Bitmap constructor's documentation clearly states that `You must keep the stream open for the lifetime of the Bitmap.` Obviously in this answer, the stream is closed even before the image was used. Refer to the documentation here: http://msdn.microsoft.com/en-us/library/z7ha67kw(v=vs.110).aspx – wenqiang Apr 15 '14 at 18:26
  • 1
    Correct, the stream viewer (wrapper to the byte[]) to the area of memory is closed, but by the that time, the bitmap now has that memory block set as the image. Disposing of the stream does not destroy the underlying memory or releases it. Not until the byte[] itself is released or claimed by the garbage collector will it be destroyed. Even in the accepted answer, they are disposing of the stream before returning the bitmap. – Wizetux Apr 25 '14 at 19:48
3
using System.IO;
byte[] img = File.ReadAllBytes(openFileDialog1.FileName);
MemoryStream ms = new MemoryStream(img);
pictureBox1.Image = Image.FromStream(ms);

or you can access like this directly,

pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);

Md Shahriar
  • 2,072
  • 22
  • 11
2

You can also convert pictureBox image to byte array like this,

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] img = ms.ToArray();
slfan
  • 8,950
  • 115
  • 65
  • 78
Md Shahriar
  • 2,072
  • 22
  • 11
1

The ImageConverter class in the System.Drawing namespace can do the conversion:

byte[] imageArray = **byte array**
ImageConverter converter = new ImageConverter();
pictureButton.Image = (Image)converter.ConvertFrom(imageArray);
DReimer
  • 71
  • 4
  • 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)` – nouman arshad Dec 27 '15 at 21:50
  • 1
    parameter not valid at last line – office 302 Dec 28 '15 at 10:10
0

If you want to use BinaryReader to convert then use like this,

FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);

BinaryReader br = new BinaryReader(fs);

byte[] img = br.ReadBytes((int)fs.Length);

Md Shahriar
  • 2,072
  • 22
  • 11
0

Try this for Converting Bitmap Images to array of bytes for jpeg pictures and png file types:

public byte[] UdfConvertPicToByte(Bitmap bitmapImages)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            
        bitmapImages.Compress(Bitmap.CompressFormat.Png, 0, stream);
            byte[] bitmapData = stream.ToArray();

            bitmap.Compress(Bitmap.CompressFormat.Jpeg, 50, stream);
            bitmapData  = stream.ToArray();              

            return bitmapData;
        }
    }