c#图像处理基础
2010-09-30 21:04:54 来源:WEB开发网5.图像的灰度化
这儿采用移位运算代替乘法运算,加快运行速度
1: /// <summary>
2: /// 图像的灰度化
3: /// gray=(r*19595 + g*38469 + b*7472) >> 16;
4: /// </summary>
5: void GrayMenuClick(object sender, EventArgs e)
6: {
7: Bitmap bmp=new Bitmap(pictureBox1.Image);
8: Bitmap newbmp=new Bitmap(pictureBox1.Image);
9: Color c=new Color();
10: Color NewC;
11: Byte r,g,b,gray;
12:
13: for (int i=0;i<bmp.Width ;i++ )
14: {
15: for (int j=0; j<bmp.Height; j++) {
16: c=bmp.GetPixel(i,j);
17: r=c.R;
18: g=c.G;
19: b=c.B;
20: gray=(Byte)((r*19595 + g*38469 + b*7472) >> 16);
21: NewC=Color.FromArgb(gray,gray,gray);
22: newbmp.SetPixel(i,j,NewC);
23: }
24: }
25:
26: // 刷新显示
27: pictureBox2.Refresh();
28: pictureBox2.Image=newbmp;
29: }
更多精彩
赞助商链接