The recent Flower manager put forward a wonderful demand, to be able to change the color of the image on the site, such as gray to color, color into gray, the feelings of the landlord you do not understand! So with the following code ...
Usage: Call the Update_pixelcolor method and pass the arguments
C # code
- #region Change the color of the picture
- <summary>
- Change the color of a picture
- </summary>
- <param name= "FilePath" > Full path of picture </param>
- <param name= "ColorIndex" > Change color, True gray, false for color </param>
- Public void Update_pixelcolor (string filePath, bool colorindex)
- {
- Bitmap bmp = new Bitmap (Bitmap.fromfile (FilePath));
- int value = 0;
- For (int i = 0; i < BMP. Height; i++)
- {
- For (int j = 0; J < BMP. Width; J + +)
- {
- if (ColorIndex)
- Value = This . Getgraynumcolor (BMP. GetPixel (J, i));
- Else
- Value = This . Gethongnumcolor (BMP. GetPixel (J, i));
- Bmp. SetPixel (J, I, Color.FromArgb (value, value, value));
- }
- }
- Bmp. Save (FilePath);
- }
- <summary>
- Get color single point pixels
- </summary>
- <param name= "POSCLR" > Single point Pixel </param>
- <returns>int</returns>
- Private int Gethongnumcolor (Color posclr)
- {
- return (POSCLR.R * 19595 + posclr.g * 38469 + posclr.b * 7472) >> 16;
- }
- <summary>
- Get gray single point pixels
- </summary>
- <param name= "POSCLR" > Single point Pixel </param>
- <returns>Color</returns>
- Private int Getgraynumcolor (Color posclr)
- {
- //To change ARGB
- return (POSCLR.R * 19595 + posclr.g * 38469 + posclr.b * 7472) >> 16;
- }
- #endregion change the color of the picture
This conversion is relatively slow to see programming life has a summary of this, which day to study