gpt4 book ai didi

C#-位图到字节数组

转载 作者:行者123 更新时间:2023-11-30 18:59:13 24 4
gpt4 key购买 nike

我有一种方法可以从面板中保存图像。此方法使用 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;
}

最佳答案

您需要使用 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();
}
}

multiple output formats to choose from, you may instead want Bmp or MemoryBmp .

关于C#-位图到字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12645705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com