2

I need save bitmap in byte[] with c#, how to do that?

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
r.r
  • 7,023
  • 28
  • 87
  • 129
  • 1
    Duplicate: http://stackoverflow.com/questions/268013/how-do-i-convert-a-bitmap-to-byte – Oleks Mar 17 '11 at 12:37

3 Answers3

7

Working code for this is

System.Drawing.Image originalImage = dpgraphic.image;// replace your image here i.e image bitmap
//Create empty bitmap image of original size
float width=0, height=0;
Bitmap tempBmp = new Bitmap((int)width, (int)height);
Graphics g = Graphics.FromImage(tempBmp);
//draw the original image on tempBmp
g.DrawImage(originalImage, 0, 0, width, height);
//dispose originalImage and Graphics so the file is now free
g.Dispose();
originalImage.Dispose();
using (MemoryStream ms = new MemoryStream())
{
    // Convert Image to byte[]
    tempBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    //dpgraphic.image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    byte[] imageBytes = ms.ToArray();
}
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
2

how about

to read in

YourByteArray = System.IO.File.ReadAllBytes( "YourGraphic.bmp" ); 

to write out

System.IO.File.WriteAllBytes( "SaveToFile.bmp", YourByteArray ); 

works for me

DRapp
  • 47,638
  • 12
  • 72
  • 142
1

Look at this sample from MSDN http://msdn.microsoft.com/en-us/library/system.drawing.imaging.bitmapdata.aspx I hope you are looking for this.

ferosekhanj
  • 1,086
  • 6
  • 11