将Bitmap.GetPixel中的某些代码更改为使用LockBits返回的直接像素缓冲区时出现问题.似乎LockBits返回的数据确实给了我与GetPixel相比不同的Color值.
这是不幸的,因为这种变化会产生不同的颜色,从而破坏自动化单元测试.我有一个29*30像素的png文件,我将Format32bppArgb加载到位图中.这真的可能是LockBits和GetPixel的数据回归是不同的.我怎么能绕过这个?
下面是一些用加载的位图重新编写它的代码
public unsafe static Bitmap Convert(Bitmap originalBitmap)
{
Bitmap converted = new Bitmap(originalBitmap);
Rectangle rect = new Rectangle(0, 0, converted.Width, converted.Height);
var locked = converted.LockBits(rect, ImageLockMode.ReadWrite, originalBitmap.PixelFormat);
byte* pData = (byte*)locked.Scan0;
// bytes per pixel
var bpp = ((int)converted.PixelFormat >> 11) & 31;
byte* r;
byte* g;
byte* b;
byte* a;
for (int y = 0; y < locked.Height; y++)
{
var row = pData + (y * locked.Stride);
for (int x = 0; x < locked.Width; x++)
{
a = row + x * bpp + 3;
r = row + x * bpp + 2;
g = row + x * bpp + 1;
b = row + x * bpp + 0;
var col = Color.FromArgb(*a, *r, *g, *b);
var origCol = originalBitmap.GetPixel(x, y);
if (origCol != col)
{
Debug.Print("Orig: {0} Pixel {1}", origCol, col);
}
}
}
converted.UnlockBits(locked);
return converted;
}
Orig: Color [A=128, R=128, G=128, B=255] Pixel Color [A=128, R=127, G=127, B=255]
Orig: Color [A=0, R=128, G=128, B=255] Pixel Color [A=0, R=0, G=0, B=0]
Orig: Color [A=45, R=128, G=128, B=255] Pixel Color [A=45, R=130, G=130, B=254]
ok -2 -2 +1
Run Code Online (Sandbox Code Playgroud)
大部分时间但似乎有一些舍入和转换正在进行.我可以强制LockBits返回GetPixel返回的数据吗?
据我所知,PNG可以包含颜色配置文件和伽马校正信息以及可能影响像素最终颜色与其原始表示的任何其他信息.
即使我们忽视有关PNG的具体知识,通常GetPixel也会返回不同于预期的值.
Bitmap.GetPixel 实施如下:
public Color GetPixel(int x, int y)
{
int color = 0;
if (x < 0 || x >= Width)
{
throw new ArgumentOutOfRangeException("x", SR.GetString(SR.ValidRangeX));
}
if (y < 0 || y >= Height)
{
throw new ArgumentOutOfRangeException("y", SR.GetString(SR.ValidRangeY));
}
int status = SafeNativeMethods.Gdip.GdipBitmapGetPixel(new HandleRef(this, nativeImage), x, y, out color);
if (status != SafeNativeMethods.Gdip.Ok)
throw SafeNativeMethods.Gdip.StatusException(status);
return Color.FromArgb(color);
}
Run Code Online (Sandbox Code Playgroud)
定义SafeNativeMethods.Gdip.GdipBitmapGetPixel是:
[DllImport(ExternDll.Gdiplus, SetLastError=true, ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Unicode)] // 3 = Unicode
[ResourceExposure(ResourceScope.None)]
internal static extern int GdipBitmapGetPixel(HandleRef bitmap, int x, int y, out int argb);
Run Code Online (Sandbox Code Playgroud)
我们从这里学到的是Gdiplus::Bitmap::GetPixel.该功能的文档说:
备注
根据位图的格式,
Bitmap::GetPixel可能不会返回与设置的值相同的值Bitmap::SetPixel.例如,如果调用Bitmap::SetPixel像素格式为的Bitmap对象,则会32bppPARGB对像素的RGB分量进行预乘.随后的调用Bitmap::GetPixel可能会因舍入而返回不同的值.此外,如果调用Bitmap::SetPixel颜色深度为每像素16位的Bitmap对象,则在转换过程中信息可能会丢失32到16位,并且后续调用Bitmap::GetPixel可能会返回不同的值.