C# BitmapSource。创建开关RGB值

C# BitmapSource。创建开关RGB值,c#,wpf,bitmapsource,C#,Wpf,Bitmapsource,我正在使用以下代码将文件中的像素加载到字节数组中: Bitmap bmp = new Bitmap(filename); var rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); IntPtr ptr = bmpData.Scan

我正在使用以下代码将文件中的像素加载到字节数组中:

Bitmap bmp = new Bitmap(filename);
var rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr ptr = bmpData.Scan0;
int numBytes = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[numBytes];
Marshal.Copy(ptr, rgbValues, 0, numBytes);
我很确定这不是问题所在。 加载文件后,我想显示和操纵颜色,然后在WPF窗口中显示

因此,我使用以下行创建位图源:

BitmapSource img = BitmapSource.Create(width, height, 96, 96, PixelFormats.Rgb24, null, pixels, stride);
问题是红色字节与绿色字节切换。 与此类似--
但是我不知道如何修复它。

然后您可以使用
PixelFormats.Bgr24
而不是
PixelFormats.Rgb24
来创建位图源

var img = BitmapSource.Create(
    width, height, 96, 96, PixelFormats.Bgr24, null, pixels, stride);

您是否尝试了
像素格式.Bgr24
而不是
像素格式.Rgb24
?为什么要做这些
System.Drawing.Bitmap
。这当然可以用纯WPF来完成。好吧,我会的,它起作用了。我想我试过了。