Use asp.net to change the image color,
Recently, the manager has raised a strange demand to change the color of pictures on the website, for example, gray becomes color and color turns gray. You don't understand the feeling of NIMA! So we have the following code...
Usage: Call the update_pixelColor method and pass parameters.
C # code
- # Region change image color
- /// <Summary>
- /// Change the image color
- /// </Summary>
- /// <Param name = "filePath"> full path of the image </param>
- /// <Param name = "colorIndex"> the color to be changed. "true" indicates the gray color, and "false" indicates 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>
- /// Obtain the color Single Point Pixel
- /// </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>
- /// Obtain the gray Single Point Pixel
- /// </Summary>
- /// <Param name = "posClr"> Single Point pixel </param>
- /// <Returns> Color </returns>
- Private int GetGrayNumColor (Color posClr)
- {
- // Change the ARGB
- Return (posClr. R * 19595 + posClr. G * 38469 + posClr. B * 7472)> 16;
- }
-
- # Change the image color using endregion
This transformation is slow. I can see the summary of this in programming life. Which day will I study?