C# 在图片框中显示三个字节数组
我有三个字节数组,存储了三种颜色(红色、绿色、蓝色),如何在c#的图片框中显示这个数组,文件类型是图像的位图文件C# 在图片框中显示三个字节数组,c#,image,rgb,picturebox,C#,Image,Rgb,Picturebox,我有三个字节数组,存储了三种颜色(红色、绿色、蓝色),如何在c#的图片框中显示这个数组,文件类型是图像的位图文件 byte[,] R=new byte[width, height]; byte[,] G=new byte[width, height]; byte[,] B=new byte[width, height]; 这三个数组不是空的,每个数组中都存储有数据。您的意思是: Bitmap bmp = new Bitmap(width,height); for(int i=0;i<wi
byte[,] R=new byte[width, height];
byte[,] G=new byte[width, height];
byte[,] B=new byte[width, height];
这三个数组不是空的,每个数组中都存储有数据。您的意思是:
Bitmap bmp = new Bitmap(width,height);
for(int i=0;i<width;i++)
for(int j=0;j<height;j++) {
SetPixel(i,j,Color.FromArgb(R[i,j],G[i,j],B[i,j]));
}
picturebox.image=bmp;
Bitmap bmp=新位图(宽度、高度);
对于(int i=0;i您必须从数据构建一个单字节数组,这不会很快,因为您必须交错数据
var bytes= new byte[width * height * 4];
for (var x = 0; x < width; x++)
for (var y = 0; y < height; y ++)
{
bytes[(x + y * width) * 4 + 1] = R[x, y];
bytes[(x + y * width) * 4 + 2] = G[x, y];
bytes[(x + y * width) * 4 + 3] = B[x, y];
}
var bmp = new Bitmap(width, height);
var data = bmp.LockBits(new Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Marshal.Copy(bytes, 0, data.Scan0, width * height * 4);
bmp.UnlockBits(data);
请注意,您应该确保始终调用bmp.UnlockBits
,因此您可能应该将其放在finally块中
这不一定是最好或最快的方法,但这取决于您的需求:)
如果你真的想用最快的方式,你可能会使用不安全的代码(不是因为它本身更快,而是因为.NET位图不是本机管理的-它是非托管位图的托管包装)。您将为非托管堆上的字节数组分配内存,然后填充数据并使用构造函数创建位图,该构造函数将IntPtr scan0
作为参数。如果操作正确,应该避免不必要的数组边界检查以及不必要的复制