Use asp.net to change the image color,

Source: Internet
Author: User

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
  1. # Region change image color
  2. /// <Summary>
  3. /// Change the image color
  4. /// </Summary>
  5. /// <Param name = "filePath"> full path of the image </param>
  6. /// <Param name = "colorIndex"> the color to be changed. "true" indicates the gray color, and "false" indicates the color. </param>
  7. Public void update_pixelColor (string filePath, bool colorIndex)
  8. {
  9. Bitmap bmp = new Bitmap (Bitmap. FromFile (filePath ));
  10. Int value = 0;
  11. For (int I = 0; I <bmp. Height; I ++)
  12. {
  13. For (int j = 0; j <bmp. Width; j ++)
  14. {
  15. If (colorIndex)
  16. Value = this. GetGrayNumColor (bmp. GetPixel (j, I ));
  17. Else
  18. Value = this. GetHongNumColor (bmp. GetPixel (j, I ));
  19. Bmp. SetPixel (j, I, Color. FromArgb (value, value, value ));
  20. }
  21. }
  22. Bmp. Save (filePath );
  23. }
  24. /// <Summary>
  25. /// Obtain the color Single Point Pixel
  26. /// </Summary>
  27. /// <Param name = "posClr"> Single Point pixel </param>
  28. /// <Returns> int </returns>
  29. Private int GetHongNumColor (Color posClr)
  30. {
  31. Return (posClr. R * 19595 + posClr. G * 38469 + posClr. B * 7472)> 16;
  32. }
  33. /// <Summary>
  34. /// Obtain the gray Single Point Pixel
  35. /// </Summary>
  36. /// <Param name = "posClr"> Single Point pixel </param>
  37. /// <Returns> Color </returns>
  38. Private int GetGrayNumColor (Color posClr)
  39. {
  40. // Change the ARGB
  41. Return (posClr. R * 19595 + posClr. G * 38469 + posClr. B * 7472)> 16;
  42. }
  43.  
  44. # 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?




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.