2

我在将一些代码从 Bitmap.GetPixel 更改为使用 LockBits 返回的直接像素缓冲区时遇到问题。与 GetPixel 相比,LockBits 返回的数据似乎确实给了我不同的颜色值。

这是不幸的,因为这种变化确实会产生不同的颜色,这会破坏自动化单元测试。我有一个 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

大多数时候,但似乎正在进行一些舍入和转换。我可以强制 LockBits 返回 GetPixel 返回的数据吗?

4

1 回答 1

5

据我所知,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);
}

的定义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);

我们从这里学到的是Gdiplus::Bitmap::GetPixel. 该功能的文档说:

评论

根据位图的格式,Bitmap::GetPixel可能不会返回与设置相同的值Bitmap::SetPixel。例如,如果调用Bitmap::SetPixel像素格式为 的 Bitmap 对象32bppPARGB,则像素的 RGB 分量会被预乘。Bitmap::GetPixel由于舍入,后续调用可能会返回不同的值。此外,如果您调用Bitmap::SetPixel颜色深度为每像素 16 位的 Bitmap 对象,则在从 32 位转换到 16 位的过程中信息可能会丢失,并且后续调用Bitmap::GetPixel可能会返回不同的值。

于 2012-10-31T09:00:36.303 回答