如何在byte [] c#中保存位图?

时间:2011-03-17 12:30:55

标签: c# bitmap byte

我需要在bitmap中使用c#保存byte[],该怎么做?

3 个答案:

答案 0 :(得分: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 :(得分:1)

从MSDN http://msdn.microsoft.com/en-us/library/system.drawing.imaging.bitmapdata.aspx查看此示例我希望您正在寻找此内容。

答案 2 :(得分:1)

怎么样

阅读

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

写出来

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

适合我