使用asp.net改變圖片顏色

來源:互聯網
上載者:User

標籤:http   color   使用   os   io   for   ar   art   

最近奇葩經理提出了奇葩的需求,要能在網站上改變圖片的顏色,比如灰色的變成彩色,彩色的變成灰色,尼瑪樓主的感受你們不懂!於是有了下面的代碼。。。

用法:調用update_pixelColor方法並傳參數即可 

C#代碼  
  1. #region 改變圖片顏色  
  2.   
  3. /// <summary>  
  4. /// 改變圖片的顏色  
  5. /// </summary>  
  6. /// <param name="filePath">圖片的完整路徑</param>  
  7. /// <param name="colorIndex">改變的顏色,true為灰色,false為彩色</param>  
  8. public void update_pixelColor(string filePath, bool colorIndex)  
  9. {  
  10.     Bitmap bmp = new Bitmap(Bitmap.FromFile(filePath));  
  11.   
  12.     int value = 0;  
  13.   
  14.     for (int i = 0; i < bmp.Height; i++)  
  15.     {  
  16.         for (int j = 0; j < bmp.Width; j++)  
  17.         {  
  18.             if (colorIndex)  
  19.                 value = this.GetGrayNumColor(bmp.GetPixel(j, i));  
  20.             else  
  21.                 value = this.GetHongNumColor(bmp.GetPixel(j, i));  
  22.   
  23.             bmp.SetPixel(j, i, Color.FromArgb(value, value, value));  
  24.         }  
  25.     }  
  26.   
  27.     bmp.Save(filePath);  
  28. }  
  29.   
  30. /// <summary>  
  31. /// 擷取彩色單點像素  
  32. /// </summary>  
  33. /// <param name="posClr">單點像素</param>  
  34. /// <returns>int</returns>  
  35. private int GetHongNumColor(Color posClr)  
  36. {  
  37.     return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;  
  38. }  
  39.   
  40. /// <summary>  
  41. /// 擷取灰色單點像素  
  42. /// </summary>  
  43. /// <param name="posClr">單點像素</param>  
  44. /// <returns>Color</returns>  
  45. private int GetGrayNumColor(Color posClr)  
  46. {  
  47.     //要改變ARGB  
  48.     return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;  
  49. }  
  50.  
  51. #endregion 改變圖片顏色  


這個轉換的比較慢 看到編程人生上有關於這方面的總結,哪天來研究一下

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.