C# 如何将BGR字节数组转换为图像

C# 如何将BGR字节数组转换为图像,c#,C#,我知道如何将字节数组转换为图像。这是我的密码: //Here create the Bitmap to the know height, width and format Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //Bitmap bmp = new Bitmap(width, height); //Create a BitmapData and Loc

我知道如何将字节数组转换为图像。这是我的密码:

//Here create the Bitmap to the know height, width and format
Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//Bitmap bmp = new Bitmap(width, height);

//Create a BitmapData and Lock all pixels to be written 
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(
                     new Rectangle(0, 0, bmp.Width, bmp.Height),
                     ImageLockMode.WriteOnly, bmp.PixelFormat);

//Copy the data from the byte array into BitmapData.Scan0
Marshal.Copy(imgDataArray, 0, bmpData.Scan0, imgDataArray.Length);
//Unlock the pixels
bmp.UnlockBits(bmpData);
bmp.Save("output1.png", System.Drawing.Imaging.ImageFormat.Png); 

但当字节数组为BGR格式时,图像颜色错误(红色为蓝色;蓝色为红色)。有人能帮我弄清楚吗?谢谢

看起来您需要遍历数组并交换每个像素的R/B值。这应该相当简单


在边缘增强标题下的文章中有代码应该可以完成这项工作。

嗯,任何速度要求都没有明确说明。应该可以很快地进行这种转换。您需要编写自己的优化图像转换函数,或者评估第三方图像处理库,直到找到一个能够快速满足您需求的库。也许你也可以用另一种格式获取源数据。