0

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

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);

问题是红色字节与绿色字节切换。与此类似 -为什么将 BitmapSource 保存到 PixelFormat.Format48bppRgb 的位图时颜色会发生变化?- 但我不知道如何解决它。

4

1 回答 1

0

然后您可以使用PixelFormats.Bgr24而不是PixelFormats.Rgb24创建 BitmapSource。

var img = BitmapSource.Create(
    width, height, 96, 96, PixelFormats.Bgr24, null, pixels, stride);
于 2013-12-15T19:48:16.830 回答