2013-04-07 42 views
1

這是我的代碼:的PixelFormat convertation麻煩

BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 
UnmanagedImage img = new UnmanagedImage(bmpData); 
//MAIN BLOCK 
BayerDithering filter = new BayerDithering(); 
img = new UnmanagedImage(img.ImageData, img.Width, img.Height, img.Stride, PixelFormat.Format8bppIndexed); 
filter.ApplyInPlace(img); 
//END MAIN BLOCK 
bitmap.UnlockBits(bmpData); 

這是結果:screenshot

爲什麼結果不complited吳?而我必須改變只有在「MAIN BLOCK」?

+0

我懷疑這段代碼會產生你給出的截圖。你正在分別使用'img'和'_img' .. – 2013-04-07 06:42:53

+0

對不起,這是我的失誤 – GLeBaTi 2013-04-07 06:52:00

+0

你把它鎖定在24bpp,你的過濾器在8bpp上應用它。你期望結果是什麼? – 2013-04-07 07:13:13

回答

2
img = new UnmanagedImage(..., img.Stride, PixelFormat.Format8bppIndexed); 

這就是問題開始的聲明。您正在傳遞像素數據和24bpp圖像的步幅,但撒謊說它是8bpp圖像。結果仍然是一張24bpp的圖片和一個徹底混淆的過濾器。

這是行不通的,從24bpp到8bpp的轉換涉及更多。 8bpp圖像不僅會有不同的像素格式和步幅,還需要一個顏色表(aka palette)。使用256種顏色創建一個合理匹配8bpp圖像的好顏色表很困難。

您將需要AForge.Imaging.ColorReduction命名空間中的一個類來完成該任務。