[C#] 濾鏡筆記 – 色調濾鏡
2012-12-03
這幾天寫到幾篇文章,其實發現都是在設定R G B 的變化..
其實帶色調濾鏡 就是將一個值拉高 將其他值降低來達到的效果..
譬如純紅濾鏡..
/// <summary>/// 純紅濾鏡/// </summary>/// <param name="bitmap">Bitmap</param>/// <param name="threshold">門檻值 -255~255</param>/// <returns></returns>public System.Drawing.Bitmap AdjustToRed(System.Drawing.Bitmap bitmap, int threshold)
{for (int y = 0; y < bitmap.Height; y++)
{for (int x = 0; x < bitmap.Width; x++)
{ // 取得每一個 pixelvar pixel = bitmap.GetPixel(x, y);
var pR = pixel.R + threshold;
pR = Math.Max(pR, 0);
pR = Math.Min(255, pR);
// 將改過的 RGB 寫回 // 只寫入紅色的值 , G B 都放零System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, pR, 0, 0);
bitmap.SetPixel(x, y, newColor);
}
}
// 回傳結果 return bitmap;}
其中比較特別的 就是我加入了門檻值 所以會提高紅色的深度
測試結果
-155 :
var img = AdjustToRed(bitmap, -155);
+155:
var img = AdjustToRed(bitmap, 155);
0:
var img = AdjustToRed(bitmap, 0);
這邊也附上 藍色 跟 綠色的 函式
綠色的濾鏡:
/// <summary>/// 純綠濾鏡/// </summary>/// <param name="bitmap"></param>/// <param name="threshold">門檻值 -255~+255</param>/// <returns></returns>public System.Drawing.Bitmap AdjustToGreen(System.Drawing.Bitmap bitmap, int threshold)
{for (int y = 0; y < bitmap.Height; y++)
{for (int x = 0; x < bitmap.Width; x++)
{ // 取得每一個 pixelvar pixel = bitmap.GetPixel(x, y);
//判斷是否超過255 如果超過就是255 var pG = pixel.G + threshold;
//如果小於0就為0 if (pG > 255) pG = 255; if (pG < 0) pG = 0; // 將改過的 RGB 寫回 // 只寫入綠色的值 , R B 都放零System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, 0, pG, 0);
bitmap.SetPixel(x, y, newColor);
}
}
// 回傳結果 return bitmap;}
藍色的濾鏡:
/// <summary>/// 純藍濾鏡/// </summary>/// <param name="bitmap"></param>/// <param name="threshold">門檻值 -255~255</param>/// <returns></returns>public System.Drawing.Bitmap AdjustToBlue(System.Drawing.Bitmap bitmap, int threshold)
{for (int y = 0; y < bitmap.Height; y++)
{for (int x = 0; x < bitmap.Width; x++)
{ // 取得每一個 pixelvar pixel = bitmap.GetPixel(x, y);
//判斷是否超過255 如果超過就是255 var pB = pixel.B + threshold;
//如果小於0就為0 if (pB > 255) pB = 255; if (pB < 0) pB = 0; // 將改過的 RGB 寫回 // 只寫入藍色的值 , R G 都放零System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, 0, 0, pB);
bitmap.SetPixel(x, y, newColor);
}
}
// 回傳結果 return bitmap;}
當然你就會想了 這種濾鏡 實用度有點低所以 就乾脆直接自訂…
/// <summary> /// 調整 RGB 色調 /// </summary> /// <param name="bitmap"></param> /// <param name="thresholdRed">紅色門檻值</param> /// <param name="thresholdBlue">藍色門檻值</param> /// <param name="thresholdGreen">綠色門檻值</param> /// <returns></returns>public System.Drawing.Bitmap AdjustToCustomColor(System.Drawing.Bitmap bitmap, int thresholdRed, int thresholdGreen, int thresholdBlue)
{for (int y = 0; y < bitmap.Height; y++)
{for (int x = 0; x < bitmap.Width; x++)
{ // 取得每一個 pixelvar pixel = bitmap.GetPixel(x, y);
//判斷是否超過255 如果超過就是255 var pG = pixel.G + thresholdGreen;
//如果小於0就為0 if (pG > 255) pG = 255; if (pG < 0) pG = 0; //判斷是否超過255 如果超過就是255 var pR = pixel.R + thresholdRed;
//如果小於0就為0 if (pR > 255) pR = 255; if (pR < 0) pR = 0; //判斷是否超過255 如果超過就是255 var pB = pixel.B + thresholdBlue;
//如果小於0就為0 if (pB > 255) pB = 255; if (pB < 0) pB = 0; // 將改過的 RGB 寫回 // 只寫入綠色的值 , R B 都放零System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, pR, pG, pB);
bitmap.SetPixel(x, y, newColor);
}
}
return bitmap;}
看一下幾個測試結果:
拉高紅色調
var img = AdjustToCustomColor(bitmap, 100,10,10);
拉高綠色調 :
var img = AdjustToCustomColor(bitmap, 10,100,10);
拉高藍色調:因為我也拉高一點點其他顏色 所以會帶有紫色效果
var img = AdjustToCustomColor(bitmap, 10,10,100);
這種方式就可以搭配作出不同色調,因為純紅純綠純藍效果會比較不常被使用..
參考文件:http://www.dotblogs.com.tw/chhuang/archive/2008/03/18/1814.aspx
