【问题标题】:Replace color of a image, using Lockbits使用 Lockbits 替换图像的颜色
【发布时间】:2016-02-07 07:41:18
【问题描述】:

首先,我会注意我接受 C# 或 VB.Net 解决方案。

我有这段旧代码,我正在尝试重构它以避免使用GetPixel/SetPixel 方法的不良习惯和性能低效:

<Extension>
Public Function ChangeColor(ByVal sender As Image, 
                            ByVal oldColor As Color, 
                            ByVal newColor As Color) As Image

    Dim bmp As New Bitmap(sender.Width, sender.Height, sender.PixelFormat)

    Dim x As Integer = 0
    Dim y As Integer = 0

    While (x < bmp.Width)

        y = 0
        While y < bmp.Height
            If DirectCast(sender, Bitmap).GetPixel(x, y) = oldColor Then
                bmp.SetPixel(x, y, newColor)
            End If
            Math.Max(Threading.Interlocked.Increment(y), y - 1)
        End While
        Math.Max(Threading.Interlocked.Increment(x), x - 1)

    End While

    Return bmp

End Function

因此,在阅读了使用 LockBits 方法的投票最多的解决方案 here 之后,我正在尝试根据我的需要调整代码,使用 Color 作为参数而不是字节序列(因为本质上是一样的):

<Extension>
Public Function ChangeColor(ByVal sender As Image, 
                            ByVal oldColor As Color, 
                            ByVal newColor As Color) As Image

   Dim bmp As Bitmap = DirectCast(sender.Clone, Bitmap)

   ' Lock the bitmap's bits.
   Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
   Dim bmpData As BitmapData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat)

    ' Get the address of the first line.
    Dim ptr As IntPtr = bmpData.Scan0

    ' Declare an array to hold the bytes of the bitmap. 
    Dim numBytes As Integer = (bmpData.Stride * bmp.Height)
    Dim rgbValues As Byte() = New Byte(numBytes - 1) {}

    ' Copy the RGB values into the array.
    Marshal.Copy(ptr, rgbValues, 0, numBytes)

    ' Manipulate the bitmap.
    For i As Integer = 0 To rgbValues.Length - 1 Step 3

      If (Color.FromArgb(rgbValues(i), rgbValues(i + 1), rgbValues(i + 2)) = oldColor) Then
          rgbValues(i) = newColor.R
          rgbValues(i + 1) = newColor.G
          rgbValues(i + 2) = newColor.B
      End If

    Next i

    ' Copy the RGB values back to the bitmap.
    Marshal.Copy(rgbValues, 0, ptr, numBytes)

    ' Unlock the bits.
    bmp.UnlockBits(bmpData)

    Return bmp

End Function

我的扩展方法有两个问题,首先是如果pixelformat不是Format24bppRgb作为原始示例那么一切都会出错,循环中抛出IndexOutOfRange异常,我想这是因为我读取 3 个字节 (RGB) 而不是 4 个 (ARGB),但我不确定如何将其调整为可以传递给函数的任何源像素格式。

其次,如果我使用 Format24bppRgb 作为原始 C# 示例,颜色将变为黑色。

请注意,我不确定我链接的 C# 问题中给出的原始解决方案是否错误,因为根据他们的 cmets 似乎在某些方面是错误的。

这是我尝试使用它的方式:

    ' This function creates a bitmap of a solid color.
    Dim srcImg As Bitmap = ImageUtil.CreateSolidcolorBitmap(New Size(256, 256), Color.Red)
    Dim modImg As Image = srcImg.ChangeColor(Color.Red, Color.Blue)

    PictureBox1.BackgroundImage = srcImg 
    PictureBox2.BackgroundImage = modImg 

【问题讨论】:

    标签: c# .net vb.net image-processing bitmap


    【解决方案1】:

    我想这是因为我正在读取 3 个字节 (RGB) 而不是 4 个 (ARGB)

    是的,这就是重点。如果要操作原始图像内容,则必须依赖PixelFormat。您必须区分索引格式(8bpp 或更少),其中BitmapData 中的像素不是颜色而是调色板的索引。

    public void ChangeColor(Bitmap bitmap, Color from, Color to)
    {
        if (Image.GetPixelFormatSize(bitmap.PixelFormat) > 8)
        {
            ChangeColorHiColoredBitmap(bitmap, from, to);
            return;
        }
    
        int indexFrom = Array.IndexOf(bitmap.Palette.Entries, from);
        if (indexFrom < 0)
            return; // nothing to change
    
        // we could replace the color in the palette but we want to see an example for manipulating the pixels
        int indexTo = Array.IndexOf(bitmap.Palette.Entries, to);
        if (indexTo < 0)
            return; // destination color not found - you can search for the nearest color if you want
    
        ChangeColorIndexedBitmap(bitmap, indexFrom, indexTo);
    }
    
    private unsafe void ChangeColorHiColoredBitmap(Bitmap bitmap, Color from, Color to)
    {
        int rawFrom = from.ToArgb();
        int rawTo = to.ToArgb();
    
        BitmapData data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.ReadWrite, bitmap.PixelFormat);
        byte* line = (byte*)data.Scan0;
        for (int y = 0; y < data.Height; y++)
        {
            for (int x = 0; x < data.Width; x++)
            {
                switch (data.PixelFormat)
                {
                    case PixelFormat.Format24bppRgb:
                        byte* pos = line + x * 3;
                        int c24 = Color.FromArgb(pos[0], pos[1], pos[2]).ToArgb();
                        if (c24 == rawFrom)
                        {
                            pos[0] = (byte)(rawTo & 0xFF);
                            pos[1] = (byte)((rawTo >> 8) & 0xFF);
                            pos[2] = (byte)((rawTo >> 16) & 0xFF);
                        }
                        break;
                    case PixelFormat.Format32bppRgb:
                    case PixelFormat.Format32bppArgb:
                        int c32 = *((int*)line + x);
                        if (c32 == rawFrom)
                            *((int*)line + x) = rawTo;
                        break;
                    default:
                        throw new NotSupportedException(); // of course, you can do the same for other pixelformats, too
                }
            }
    
            line += data.Stride;
        }
    
        bitmap.UnlockBits(data);
    }
    
    private unsafe void ChangeColorIndexedBitmap(Bitmap bitmap, int from, int to)
    {
        int bpp = Image.GetPixelFormatSize(bitmap.PixelFormat);
        if (from < 0 || to < 0 || from >= (1 << bpp) || to >= (1 << bpp))
            throw new ArgumentOutOfRangeException();
    
        if (from == to)
            return;
    
        BitmapData data = bitmap.LockBits(
            new Rectangle(Point.Empty, bitmap.Size),
            ImageLockMode.ReadWrite,
            bitmap.PixelFormat);
    
        byte* line = (byte*)data.Scan0;
    
        // scanning through the lines
        for (int y = 0; y < data.Height; y++)
        {
            // scanning through the pixels within the line
            for (int x = 0; x < data.Width; x++)
            {
                switch (bpp)
                {
                    case 8:
                        if (line[x] == from)
                            line[x] = (byte)to;
                        break;
                    case 4:
                        // First pixel is the high nibble. From and To indices are 0..16
                        byte nibbles = line[x / 2];
                        if ((x & 1) == 0 ? nibbles >> 4 == from : (nibbles & 0x0F) == from)
                        {
                            if ((x & 1) == 0)
                            {
                                nibbles &= 0x0F;
                                nibbles |= (byte)(to << 4);
                            }
                            else
                            {
                                nibbles &= 0xF0;
                                nibbles |= (byte)to;
                            }
    
                            line[x / 2] = nibbles;
                        }
                        break;
                    case 1:
                        // First pixel is MSB. From and To are 0 or 1.
                        int pos = x / 8;
                        byte mask = (byte)(128 >> (x & 7));
                        if (to == 0)
                            line[pos] &= (byte)~mask;
                        else
                            line[pos] |= mask;
                        break;
                }
            }
    
            line += data.Stride;
        }
    
        bitmap.UnlockBits(data);
    }
    

    【讨论】:

    • 所以我添加了 1、4、8、24 和 32 bpp 像素格式的示例。当然,您也可以计算 15/16/64 和其他格式的颜色。我在这里使用了不安全的方法,但是您可以使用Marshal.Copy 通过托管数组将数据复制到那里并返回。
    【解决方案2】:

    您发布的代码中存在三个不同的问题:

    1. 您的颜色组件顺序错误。 Bitmap 类以小端格式将像素值存储为整数。这意味着组件的字节顺序实际上是 BGR(或 32bpp 的 BGRA)。
    2. 在 VB.NET 中,不能直接比较 Color 的值。我对 VB.NET 知之甚少,不知道为什么会这样,但我认为这是与 VB.NET 如何处理值类型有关的正常语言行为。要正确比较Color的值,需要调用ToArgb(),它会返回一个Integer的值,可以直接比较。
    3. 您的For 循环使用了错误的结束值。如果只从数组的长度中减去1,那么循环可能会在行尾运行到填充,但是发现字节太少而无法成功将2添加到循环索引并且仍然留在数组中。

    这是一个适合我的扩展方法版本:

    <Extension>
    Public Function ChangeColor(ByVal image As Image, ByVal oldColor As Color, ByVal newColor As Color)
        Dim newImage As Bitmap = New Bitmap(image.Width, image.Height, image.PixelFormat)
    
        Using g As Graphics = Graphics.FromImage(newImage)
            g.DrawImage(image, Point.Empty)
        End Using
    
        ' Lock the bitmap's bits.
        Dim rect As New Rectangle(0, 0, newImage.Width, newImage.Height)
        Dim bmpData As BitmapData = newImage.LockBits(rect, ImageLockMode.ReadWrite, newImage.PixelFormat)
    
        ' Get the address of the first line.
        Dim ptr As IntPtr = bmpData.Scan0
    
        ' Declare an array to hold the bytes of the bitmap. 
        Dim numBytes As Integer = (bmpData.Stride * newImage.Height)
        Dim rgbValues As Byte() = New Byte(numBytes - 1) {}
    
        ' Copy the RGB values into the array.
        Marshal.Copy(ptr, rgbValues, 0, numBytes)
    
        ' Manipulate the bitmap.
        For i As Integer = 0 To rgbValues.Length - 3 Step 3
    
            Dim testColor As Color = Color.FromArgb(rgbValues(i + 2), rgbValues(i + 1), rgbValues(i))
    
            If (testColor.ToArgb() = oldColor.ToArgb()) Then
                rgbValues(i) = newColor.B
                rgbValues(i + 1) = newColor.G
                rgbValues(i + 2) = newColor.R
            End If
    
        Next i
    
        ' Copy the RGB values back to the bitmap.
        Marshal.Copy(rgbValues, 0, ptr, numBytes)
    
        ' Unlock the bits.
        newImage.UnlockBits(bmpData)
    
        Return newImage
    
    End Function
    

    就目前而言:

    我不确定如何调整它以适应我可以传递给函数的任何源像素格式。

    不幸的是,API 不直接返回位图的每像素位数或每像素字节数。您可以概括您的代码以考虑每个像素的字节数,但您仍然必须至少将 PixelFormat 值映射到每个像素值的字节数。

    【讨论】:

    • 谢谢您的回答!在这种情况下,我将评估一些像素格式,然后为未知像素格式抛出 NotImplemented 异常。您能否提及或提供一个 url(来自维基百科?MSDN 或其他来源)在哪里知道常见像素格式的每个字节/像素?此刻我有:PixelFormat.Format24bppRgb = 3, PixelFormat.Format32bppArgb = 4,PixelFormat.Format32bppRgb = 4
    • 似乎在 WPF 中我们可以获得每像素位数:msdn.microsoft.com/en-us/library/…
    猜你喜欢
    • 1970-01-01
    • 2018-05-11
    • 2010-12-05
    • 2011-07-12
    • 1970-01-01
    • 2011-07-19
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多