Change the color of a picture using ASP.

Source: Internet
Author: User

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
  1. #region Change the color of the picture
  2. <summary>
  3. Change the color of a picture
  4. </summary>
  5. <param name= "FilePath" > Full path of picture </param>
  6. <param name= "ColorIndex" > Change color, True gray, false for 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. Get color single point pixels
  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. Get gray single point pixels
  35. </summary>
  36. <param name= "POSCLR" > Single point Pixel </param>
  37. <returns>Color</returns>
  38. Private int Getgraynumcolor (Color posclr)
  39. {
  40. //To change ARGB
  41. return (POSCLR.R * 19595 + posclr.g * 38469 + posclr.b * 7472) >> 16;
  42. }
  43. #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

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.