Skip to main content
 首页 » 编程设计

c#之C#中位图图像的灰度变暗问题

2024年02月24日15wayfarer

我正在将 8bppIndexed 灰度图像加载到内存中并读取像素值。问题是我从像素获得的值似乎与实际图像不匹配,它们总是更暗。我的图像是一个简单的灰色渐变,如下所示:

alt text

右下像素返回 191,左上像素返回 0。左上实际上是 64,右下是 255。

这是我加载图片的方式:

Bitmap threshImg = new Bitmap(@"C:\grey.bmp"); 

检查 PixelFormat 确认它在 Format8bppIndexed 中。所以我像这样阅读右下角和左上角的像素:

BitmapData data = threshImg.LockBits(new Rectangle(0, 0, rectWidth, rectHeight), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed); 
unsafe 
{ 
    byte* pixel = (byte*)data.Scan0.ToPointer(); 
    int topVal = (int)(byte)pixel[0]; 
    int bottomVal = (int)(byte)pixel[((threshImg.Height * threshImg.Width)) - 1]; 
} 
threshImg.UnlockBits(data); 

如果我将图像转换为 24bppRbg(并相应地调整代码),我会在各个角看到正确的颜色值。

有人知道为什么我在使用 8bppIndexed 图像时会得到较暗的值吗?

请您参考如下方法:

8bpp 索引图像中的值不是颜色本身(或灰度值)而是索引。尝试在调色板中查找颜色值。