2011-03-17 48 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); 

作品對我來說