Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put image in a picture box from a byte[] in C#

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?

like image 976
Kevin Avatar asked Mar 06 '12 01:03

Kevin


1 Answers

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
like image 83
John Woo Avatar answered Oct 21 '22 11:10

John Woo