I am loading pixels from a file onto an array of bytes using this code:
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);
I am pretty sure that this is not the problem. After I have loaded the file, I want to display manipulate the colors and then display in a WPF window.
So I create a bitmap source with the following line:
BitmapSource img = BitmapSource.Create(width, height, 96, 96, PixelFormats.Rgb24, null, pixels, stride);
The problem is that the red bytes are switched with the green bytes. It is similar to this - Why color changes when save a BitmapSource to Bitmap for PixelFormat.Format48bppRgb? - but i don't know how to fix it.
Then you may use
PixelFormats.Bgr24
instead ofPixelFormats.Rgb24
to create the BitmapSource.