c# 快速修改图片颜色

  public static  void ChangeColour(this Bitmap bmp, byte inColourR, byte inColourG, byte inColourB, byte outColourR, byte outColourG, byte outColourB)
        {
            // Specify a pixel format.
            PixelFormat pxf = PixelFormat.Format24bppRgb;

            // Lock the bitmap's bits.
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmpData =
            bmp.LockBits(rect, ImageLockMode.ReadWrite,
                         pxf);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap. 
            // int numBytes = bmp.Width * bmp.Height * 3; 
            int numBytes = bmpData.Stride * bmp.Height;
            byte[] rgbValues = new byte[numBytes];

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

            // Manipulate the bitmap
            for (int counter = 0; counter < rgbValues.Length; counter += 3)
            {
                if (rgbValues[counter] != inColourR &&
                    rgbValues[counter + 1] != inColourG &&
                    rgbValues[counter + 2] != inColourB)
                {
                    rgbValues[counter] = outColourR;
                    rgbValues[counter + 1] = outColourG;
                    rgbValues[counter + 2] = outColourB;
                }
            }

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

            // Unlock the bits.
            bmp.UnlockBits(bmpData);
        }
  public unsafe void ChangeColour(ref Bitmap bmp, byte inColourR, byte inColourG, byte inColourB, byte outColourR, byte outColourG, byte outColourB, int k)
        {
            // Specify a pixel format.
            PixelFormat pxf = PixelFormat.Format24bppRgb;

            // Lock the bitmap's bits.
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmpData =
            bmp.LockBits(rect, ImageLockMode.ReadWrite,
                         pxf);

            // Get the address of the first line.
            var bp =(byte*) bmpData.Scan0.ToPointer();
            for (int i = 0; i != bmpData.Height; i++)
            {
                for (int j = 0; j != bmpData.Width; j++)
                {
                    //0.3R+0.59G+0.11B
                    if (bp[i * bmpData.Stride + j * 3] == inColourB && bp[i * bmpData.Stride + j * 3 + 1] == inColourG && bp[i * bmpData.Stride + j * 3 + 2] == inColourR)
                    {
                        bp[i * bmpData.Stride + j * 3] = outColourB;
                        bp[i * bmpData.Stride + j * 3 + 1] = outColourG;
                        bp[i * bmpData.Stride + j * 3 + 2] = outColourR;
                        //float value = 0.11F * bp[i * bmpData.Stride + j * 3] + 0.59F * bp[i * bmpData.Stride + j * 3 + 1] +
                        //              0.3F * bp[i * bmpData.Stride + j * 3 + 2];
                    }
                }
            }

            bmp.UnlockBits(bmpData);
            
        }
原文地址:https://www.cnblogs.com/liuxinls/p/3338243.html