Image To Byte Array Function

sandeepparekh9 0 Tallied Votes 276 Views Share

simple Code Snippets to Convert Image to Byte Array and Vise Versa

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
} 

public Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}
de Source 0 Light Poster

can you explain it plz
i mean any example

sandeepparekh9 109 Posting Whiz

you can basically use it to store images to sql database


see here:http://www.codeproject.com/KB/database/ImageSaveInDataBase.aspx

before storing image to database it has to be converted to byte array.. rather than writing this code to everytime you need to store image just call this function ..

de Source 0 Light Poster

good work thnks

CsharpChico 1 Junior Poster in Training

public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}

Great code but when passing back to image you can save one line of code like this

public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
return Image.FromStream(ms);
}
sandeepparekh9 109 Posting Whiz

Great code but when passing back to image you can save one line of code like this

nice suggestion .thankx

Teme64 215 Veteran Poster

And here's my VB.NET blog post "Convert image to byte array and vice versa" back from 2008. It has some additional comments to consider also with the C# version above.

CsharpChico 1 Junior Poster in Training

nice suggestion .thankx

No Problem hope it helps:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.