Use asp. net to change the image color such as gray to color

  • 2021-01-14 05:47:58
  • OfStack

Recently the strange manager put forward the strange demand, to be able to change the color of the picture on the website, such as gray into color, color into gray, you do not understand the feelings of the Nima building master! Hence the following code.

Usage: Call the update_pixelColor method and pass an argument


#region  Change the color of the image  

/// <summary> 
///  Change the color of the picture  
/// </summary> 
/// <param name="filePath"> The full path of the image </param> 
/// <param name="colorIndex"> Change the color, true As the gray, false For the 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> 
///  Gets a color single point pixel  
/// </summary> 
/// <param name="posClr"> A single point of pixel </param> 
/// <returns>int</returns> 
private int GetHongNumColor(Color posClr) 
{ 
return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16; 
} 

/// <summary> 
///  Gets a gray single point pixel  
/// </summary> 
/// <param name="posClr"> A single point of pixel </param> 
/// <returns>Color</returns> 
private int GetGrayNumColor(Color posClr) 
{ 
// To change the ARGB 
return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16; 
} 

#endregion  Change the color of the image  

Related articles: