[英]C#-Bitmap to Byte array
I have a method which saves the image from a panel. 我有一种方法可以保存面板中的图像。 This method is using Bitmap class.
此方法使用Bitmap类。 I wants that my method should return byte array of the image.
我希望我的方法应该返回图像的字节数组。
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;
}
You'll need to use a MemoryStream to serialize the bitmap to an image format and get the bytes; 您需要使用MemoryStream将位图序列化为图像格式并获取字节;
using (Bitmap bitmap = new Bitmap(500, 500))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
...
}
using (var memoryStream = new MemoryStream())
{
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
return memoryStream.ToArray();
}
}
There are multiple output formats to choose from, you may instead want Bmp or MemoryBmp . 有多种输出格式可供选择,您可能需要Bmp或MemoryBmp 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.