程序很简单
1
/// <summary>
2
/// 将彩色图片变成黑白色的照片
3
/// </summary>
4
/// <param name="image">原来图片</param>
5
/// <returns>返回的黑白照片</returns>
6
public static Bitmap WhiteAndBlack(System.Drawing.Bitmap image)
7
{
8
//原来图片的长度
9
int width = image.Width;
10
//原来图片的高度
11
int height = image.Height;
12
//改变色素
13
//横坐标
14
for (int x = 0; x < width; x++)
15
{
16
//纵坐标
17
for (int y = 0; y < height; y++)
18
{
19
//获得坐标(x,y)颜色
20
Color color = image.GetPixel(x, y);
21
//获得该颜色下的黑白色
22
int value = (color.R + color.G + color.B) / 3;
23
//设置颜色
24
image.SetPixel(x,y,Color.FromArgb(value, value, value));
25
}
26
}
27
return image;
28
}
测试效果:
/// <summary>2
/// 将彩色图片变成黑白色的照片3
/// </summary>4
/// <param name="image">原来图片</param>5
/// <returns>返回的黑白照片</returns>6
public static Bitmap WhiteAndBlack(System.Drawing.Bitmap image)7
{8
//原来图片的长度9
int width = image.Width;10
//原来图片的高度11
int height = image.Height;12
//改变色素13
//横坐标14
for (int x = 0; x < width; x++)15
{16
//纵坐标17
for (int y = 0; y < height; y++)18
{19
//获得坐标(x,y)颜色20
Color color = image.GetPixel(x, y);21
//获得该颜色下的黑白色22
int value = (color.R + color.G + color.B) / 3;23
//设置颜色24
image.SetPixel(x,y,Color.FromArgb(value, value, value));25
}26
}27
return image;28
}原图:
黑白:

