C# 像素格式转换故障
这是我的代码:C# 像素格式转换故障,c#,.net,image-processing,aforge,pixelformat,C#,.net,Image Processing,Aforge,Pixelformat,这是我的代码: 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 BayerDithe
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);
这就是结果:
为什么结果不完整?在“主块”中我必须更改的内容是什么
这就是问题的起因。您正在传递像素数据和24bpp图像的步幅,但撒谎说这是8bpp图像。结果仍然是一个24bpp的图像和一个完全混乱的过滤器
这是行不通的,从24bpp到8bpp的转换要复杂得多。8bpp图像不仅具有不同的像素格式和步幅,还需要颜色表(也称调色板)。创建一个具有256种颜色的良好颜色表以生成合理匹配的8bpp图像是很困难的
您需要AForge.Imaging.ColorReduce命名空间中的一个类来完成这项工作。我怀疑这段代码是否会生成您给出的屏幕截图。您正在分别使用
img
和\u img
。抱歉,这是我的失误您将其锁定在24bpp,而您的筛选器将其应用在8bpp。。你认为结果会怎样?我不能改变“主块”之外的任何东西/
img = new UnmanagedImage(..., img.Stride, PixelFormat.Format8bppIndexed);