2013-12-15 38 views
0

我加載從文件像素到使用此代碼的字節的數組:BitmapSource.Create切換RGB值

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

的問題是,紅色字節與綠色字節交換。 它與此類似 - Why color changes when save a BitmapSource to Bitmap for PixelFormat.Format48bppRgb? - 但我不知道如何解決它。

+0

你試過'PixelFormats.Bgr24'而不是'PixelFormats.Rgb24'嗎?爲什麼所有這些'System.Drawing.Bitmap'的東西。這當然可以用純WPF來完成。 – Clemens

+0

嗯,我會的,它的工作。我以爲我嘗試過。 – phil

回答

0

然後,您可以使用PixelFormats.Bgr24而不是PixelFormats.Rgb24來創建BitmapSource。

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