954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Image To Byte Array Function

0
By sandeepparekh9 on May 26th, 2011 1:16 pm

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;
}

can you explain it plz
i mean any example

de Source
Light Poster
30 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 


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 ..

sandeepparekh9
Posting Whiz
367 posts since Dec 2010
Reputation Points: 123
Solved Threads: 71
 

good work thnks

de Source
Light Poster
30 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 
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);
}
CsharpChico
Junior Poster in Training
72 posts since May 2010
Reputation Points: 12
Solved Threads: 8
 
Great code but when passing back to image you can save one line of code like this

nice suggestion .thankx

sandeepparekh9
Posting Whiz
367 posts since Dec 2010
Reputation Points: 123
Solved Threads: 71
 

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.

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 
nice suggestion .thankx

No Problem hope it helps:)

CsharpChico
Junior Poster in Training
72 posts since May 2010
Reputation Points: 12
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You