彩色圖像轉換爲黑白圖像時需要計算圖像中每像素有效的亮度值,通過匹配像素
亮度值可以輕鬆轉換爲黑白圖像。
計算像素有效的亮度值可以使用下面的公式:
Y=0.3RED+0.59GREEN+0.11Blue
然後使用 Color.FromArgb(Y,Y,Y) 來把計算後的值轉換
當然了這是一個好的方法,如果需要更簡單的做到圖像的色彩轉換還可以使用ColorMatrix類
public Bitmap ConvertToGrayscale(Bitmap source)
{
Bitmap bm = new Bitmap(source.Width, source.Height);
for (int y = 0; y < bm.Height; y++)
{
for (int x = 0; x < bm.Width; x++)
{
Color c = source.GetPixel(x, y);
int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
}
}
return bm;
}