zoukankan      html  css  js  c++  java
  • 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);
                
            }
  • 相关阅读:
    SSL证书安装指引
    腾讯云中ssL证书的配置安装
    微信小程序:微信登陆(ThinkPHP作后台)
    TPshop学习(8)微信支付
    LNMP安装Let’s Encrypt 免费SSL证书方法:自动安装与手动配置Nginx
    ruby文档
    tesseract-ocr图片识别开源工具
    Python读写文件
    百度贴吧的网络爬虫(v0.4)源码及解析
    中文分词库
  • 原文地址:https://www.cnblogs.com/liuxinls/p/3338243.html
Copyright © 2011-2022 走看看