tags:

views:

87

answers:

4

hello expert

I'm new in C# is any body Suggest me how can i convert the image into byte array..and vise versa ..if any body have some code sample help me out i m develop a wpf application and enable to find stream reader.

thanks in advance shashank

+1  A: 

Do you only want the pixels or the whole image (including headers) as an byte array?

For pixels: Use the CopyPixels method on Bitmap. Something like:

var bitmap = new BitmapImage(uri);

//Pixel array
byte[] pixels = new byte[width * height * 4]; //account for stride if necessary and whether the image is 32 bit, 16 bit etc.

bitmap.CopyPixels(..size, pixels, fullStride, 0); 
lasseespeholt
+1  A: 

try this:

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

public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
anishmarokey
imageToByteArray(System.Drawing.Image imageIn) imageIn is image path or anything else how we can pass image in this
Chunmun Tyagi
This is what I do anytime I need to convert an image to a byte array or back.
Alex Essilfie
+1  A: 

Hi, You can use File.ReadAllBytes() method to read any file into byte array. To write byte array into file, just use File.WriteAllBytes() method. Hope this helps. You can find more information and sample code here.

Shekhar