2011-03-17 51 views
2

我需要保存bitmapbyte[]与C#,如何做到这一点?此如何在byte [] c#中保存位图?

+1

重复:http://stackoverflow.com/questions/268013/怎么办 - 我 - 转换 - 一个位图到字节 – Alex 2011-03-17 12:37:30

回答

7

工作的代码是

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(); 
} 
1

怎么样

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

读写出来

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

作品对我来说