首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bitmap.LockBits给出了错误的值?

Bitmap.LockBits给出了错误的值?
EN

Stack Overflow用户
提问于 2012-10-31 16:48:21
回答 1查看 1.3K关注 0票数 2

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

这是不幸的,因为这种更改确实会产生不同的颜色,这将破坏自动化的单元测试。我有一个29*30像素的png文件,我将Format32bppArgb加载到位图中。这真的是LockBits和GetPixel返回的数据不同吗?我怎么才能避免这个问题呢?

下面是用加载的位图重现它的一些代码

代码语言:javascript
复制
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

大多数时间,但似乎有一些舍入和转换正在进行。我能像GetPixel那样强制LockBits返回数据吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-31 17:00:36

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

即使我们忽略关于PNG的特定知识,一般来说,GetPixel可能会返回与预期不同的值。

Bitmap.GetPixel的实现方式如下:

代码语言:javascript
复制
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的定义是:

代码语言:javascript
复制
[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对象调用Bitmap::SetPixel,则在从32位到16位的转换过程中可能会丢失信息,后续调用Bitmap::GetPixel可能会返回不同的值。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13154172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档