Asp.net(C#)-彩色图片转化为黑白
/// <summary>
/// 彩色图片转化为黑白
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static Bitmap ConvertToGrayscale(Bitmap bitmap)
{
Bitmap bm = new Bitmap(bitmap.Width, bitmap.Height);
for (int y = 0; y < bm.Height; y++)
{
for (int x = 0; x < bm.Width; x++)
{
Color c = bitmap.GetPixel(x, y);
int rgb = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
bm.SetPixel(x, y, Color.FromArgb(rgb, rgb, rgb));
}
}
return bm;
}
/// 彩色图片转化为黑白
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static Bitmap ConvertToGrayscale(Bitmap bitmap)
{
Bitmap bm = new Bitmap(bitmap.Width, bitmap.Height);
for (int y = 0; y < bm.Height; y++)
{
for (int x = 0; x < bm.Width; x++)
{
Color c = bitmap.GetPixel(x, y);
int rgb = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
bm.SetPixel(x, y, Color.FromArgb(rgb, rgb, rgb));
}
}
return bm;
}