程序很简单
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
}
测试效果:
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

原图:
黑白: