gpt4 book ai didi

c# - 从字节数组创建一个 PNG

转载 作者:行者123 更新时间:2023-12-04 01:59:56 25 4
gpt4 key购买 nike

我有一个一维字节数组,A、R、G 和 B 具有单独的值,并且我知道预期图像的高度和宽度。如何编码此数据并将其另存为 PNG?

最佳答案

byte[] data = new byte[] {
255, 255, 000, 000, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 000, 000,
255, 255, 255, 255, 255, 255, 000, 000, 255, 255, 255, 255, 255, 255, 000, 000, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 000, 000, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 000, 000, 255, 255, 255, 255, 255, 255, 000, 000, 255, 255, 255, 255,
255, 255, 000, 000, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 000, 000
};

Bitmap bmp = new Bitmap(5, 5);
for (int y = 0; y < 5; ++y)
for (int x = 0; x < 5; ++x)
{
int offset = y * 5 * 4 + x * 4;
bmp.SetPixel(x, y, Color.FromArgb(data[offset], data[offset + 1], data[offset + 2], data[offset + 3]));
}
bmp.Save(@"c:\tmp.png");
}

如果数组中的值以这种方式排序:B G R A B G R A B G R A ...您可以使用以下代码,它应该更快:
byte[] data = new byte[] {
// B G R A B G R A B G R A
0, 0, 255, 255, 0, 0, 0, 255, 0, 255, 0, 255,
0, 0, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255,
0, 255, 0, 255, 0, 0, 0, 255, 255, 0, 0, 255
};
int width = 3;
int height = 3;

Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var bitmapData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
Marshal.Copy(data, 0, bitmapData.Scan0, data.Length);
bmp.UnlockBits(bitmapData);
bmp.Save(@"c:\tmp.png");

此图像应如下所示: enter image description here

关于c# - 从字节数组创建一个 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32203347/

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