Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在图片框中显示三个字节数组_C#_Image_Rgb_Picturebox - Fatal编程技术网

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

我有三个字节数组,存储了三种颜色(红色、绿色、蓝色),如何在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<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
作为参数。如果操作正确,应该避免不必要的数组边界检查以及不必要的复制