Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save bitmap in byte[] c#?

Tags:

c#

bitmap

byte

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

like image 200
r.r Avatar asked Jun 05 '26 00:06

r.r


2 Answers

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();
}
like image 162
Pranay Rana Avatar answered Jun 07 '26 12:06

Pranay Rana


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

like image 33
DRapp Avatar answered Jun 07 '26 13:06

DRapp