標籤:http color 使用 os io for ar art
最近奇葩經理提出了奇葩的需求,要能在網站上改變圖片的顏色,比如灰色的變成彩色,彩色的變成灰色,尼瑪樓主的感受你們不懂!於是有了下面的代碼。。。
用法:調用update_pixelColor方法並傳參數即可
C#代碼
- #region 改變圖片顏色
-
- /// <summary>
- /// 改變圖片的顏色
- /// </summary>
- /// <param name="filePath">圖片的完整路徑</param>
- /// <param name="colorIndex">改變的顏色,true為灰色,false為彩色</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>
- /// 擷取彩色單點像素
- /// </summary>
- /// <param name="posClr">單點像素</param>
- /// <returns>int</returns>
- private int GetHongNumColor(Color posClr)
- {
- return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
- }
-
- /// <summary>
- /// 擷取灰色單點像素
- /// </summary>
- /// <param name="posClr">單點像素</param>
- /// <returns>Color</returns>
- private int GetGrayNumColor(Color posClr)
- {
- //要改變ARGB
- return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
- }
-
- #endregion 改變圖片顏色
這個轉換的比較慢 看到編程人生上有關於這方面的總結,哪天來研究一下