我正在将一个8像素的灰度图像加载到内存中并读取像素值。问题是我从像素中得到的值似乎与实际图像不匹配,它们总是较暗。我的图像是一个简单的灰色渐变,如下所示:
右下角的像素返回191,左上角返回0。左上角实际上是64,右下角是255。
以下是我加载图像的方式:
Bitmap threshImg = new Bitmap(@"C:\grey.bmp");
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(并相应地调整代码),我会在各个角落看到正确的颜色值。
有人知道为什么我在使用8Bppined图像时会得到更暗的值吗?