本函数完成的功能是对图像进行灰度处理,我们的基本想法可是将每个象素点的三种颜色成分的值取平均值。然而由于人眼的敏感性,这样完全取平均值的做法的效果并不好,所以在程序中我取了三个效果最好的参数:.299,.587,.114。不过在这里要向读者指明的是,在GDI+中图像存储的格式是BGR而非RGB,即其顺序为:Blue、Green、Red。所以在for循环内部一定要设置好red、green、blue等变量的值,切不可颠倒。函数执行成功后,同样返回true值。
1 public static bool Gray(Bitmap b)
2 {
3 BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
4 ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
5 int stride = bmData.Stride;
6 System.IntPtr Scan0 = bmData.Scan0;
7 unsafe
8 {
9 byte * p = (byte *)(void *)Scan0;
10 int nOffset = stride - b.Width*3;
11 byte red, green, blue;
12 for(int y=0;y<b.Height;++y)
13 {
14 for(int x=0; x < b.Width; ++x )
15 {
16 blue = p[0];
17 green = p[1];
18 red = p[2];
19 p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);
20 p += 3;
21 }
22 p += nOffset;
23 }
24 }
25 b.UnlockBits(bmData);
26 return true;
27 }
2 {
3 BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
4 ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
5 int stride = bmData.Stride;
6 System.IntPtr Scan0 = bmData.Scan0;
7 unsafe
8 {
9 byte * p = (byte *)(void *)Scan0;
10 int nOffset = stride - b.Width*3;
11 byte red, green, blue;
12 for(int y=0;y<b.Height;++y)
13 {
14 for(int x=0; x < b.Width; ++x )
15 {
16 blue = p[0];
17 green = p[1];
18 red = p[2];
19 p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);
20 p += 3;
21 }
22 p += nOffset;
23 }
24 }
25 b.UnlockBits(bmData);
26 return true;
27 }