2
我有一個方法可以從面板保存圖像。此方法使用Bitmap類。我希望我的方法應該返回圖像的字節數組。C#-Bitmap到字節數組
private byte[] SaveImage()
{
byte[] byteContent = null;
using (Bitmap bitmap = new Bitmap(500, 500))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
Rectangle rectangle = myPanel.Bounds;
Point sourcePoints = myPanel.PointToScreen(new Point(myPanel.ClientRectangle.X, myPanel.ClientRectangle.Y));
g.CopyFromScreen(sourcePoints, Point.Empty, rectangle.Size);
}
string fileName = @"E:\\MyImages.Jpg";
bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
return byteContent;
}
你爲什麼要返回空字節內容?在代碼中你使用或分配byteContent ..? – MethodMan
將位圖保存到內存流並設置byteContent = stream.ToArray(); –
http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array-in-c – Kevin