Image to ByteArray and ByteArray to Image using C#

Image to Byte[]
----------------

private static byte[] GetImageByteArr(System.Drawing.Image img)
{
byte[] ImgByte;
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
ImgByte = stream.ToArray();
}
return ImgByte;
}


Byte[] to Image
----------------

private Image bytetoimg(byte[] bytearr)
{
Image newImage;
MemoryStream ms=new MemoryStream(bytearr,0,bytearr.Length);
ms.Write(bytearr,0,bytearr.Length);
newImage=Image.FromStream(ms,true);
return newImage;
}


...S.Vinothkumar

1 comment:

Padmakar said...

Hi,

I tried converting bytearray to image using the method u suggested. It is throwing the execption "Argument exception :Parameter is not valid."
Can you please provide us a solution for this.