【问题标题】:Bitmap.LockBits giving wrong values?Bitmap.LockBits 给出错误的值?
【发布时间】:2012-10-20 16:31:25
【问题描述】:

我在将一些代码从 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 返回的数据吗?

【问题讨论】:

  • PNG 不能包含颜色配置文件/伽马设置/其他吗?也许GetPixel 会根据颜色配置文件检索像素的颜色。显然原始 ARGB 值无法存储颜色配置文件,因此您会看到不同的结果。
  • 您的 bpp 变量试图处理像素格式。但实际上不能工作,除非它是 32bpp 图像。至少在 LockBits() 调用中询问您需要的像素格式。 PArgb 将是一个问题。它也可以通过在原始图像上使用 LockBits() 来工作。但是代码已经没有任何意义了。
  • 我确实在我的测试数据中发现了一些 24/32bpp 图像。是的,我拒绝其他一切。 bpp 变量提取代码实际上根本不起作用,因为它确实报告了错误的字节数。现在我有了一个支持格式的开关盒。

标签: c# windows gdi


【解决方案1】:

据我所知,PNG 可以包含颜色配置文件和 gamma 校正信息以及任何其他可能影响像素最终颜色与其原始表示的其他内容。

即使我们忽略有关 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);

我们从here 学到的是Gdiplus::Bitmap::GetPixel。该函数的文档说:

备注

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

【讨论】:

  • 好吧,似乎没有办法解决这个问题。我的图片格式不是 PARGB,但似乎 PNG 也有类似的情况。
  • @AloisKraus PARGB 只是一个例子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
  • 2019-07-15
  • 2012-05-03
  • 1970-01-01
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多