2013-04-07 55 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命名空间中的一个类来完成该任务。