gpt4 book ai didi

c# - 字节数组或矩阵到 BitMap

转载 作者:太空狗 更新时间:2023-10-29 21:09:49 24 4
gpt4 key购买 nike

我目前遇到以下问题:我想转换来自具有以下配置的文件的字节数组:

Byte1: R color of pixel 0,0.
Byte2: G color of pixel 0,0.
Byte3: B color of pixel 0,0.
Byte4: R color of pixel 0,1.

...
ByteN: R color of pixel n,n.

所以我想做的是将这些字节转换为位图,而不必使用 bitmap.setPixel 逐个像素地设置,因为这需要很长时间。

有什么建议吗?提前致谢!

最佳答案

如果您有像素的 byte[] 以及宽度和高度,那么您可以使用 BitmapData 将字节写入位图,因为您也知道格式。这是一个例子:

//Your actual bytes
byte[] bytes = {255, 0, 0, 0, 0, 255};
var width = 2;
var height = 1;
//Make sure to clean up resources
var bitmap = new Bitmap(width, height);
var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
bitmap.UnlockBits(data);

这是一个非常快速的操作。

您至少需要在 C# 文件的顶部导入这三个命名空间:

using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

关于c# - 字节数组或矩阵到 BitMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10933791/

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