算法如下:
1 private void button1_Click(object sender, EventArgs e) 2 { 3 int Height = this.pictureBox1.Image.Height; 4 int Width = this.pictureBox1.Image.Width; 5 Bitmap bitmap = new Bitmap(Width, Height); 6 Bitmap MyBitmap = (Bitmap)this.pictureBox1.Image; 7 Color pixel; 8 for (int x = 0; x < Width; x++) 9 for (int y = 0; y < Height; y++) 10 { 11 pixel = MyBitmap.GetPixel(x, y); 12 int r, g, b, Result = 0; 13 r = pixel.R; 14 g = pixel.G; 15 b = pixel.B; 16 //实例程序以加权平均值法产生黑白图像 17 int iType = 2; 18 switch (iType) 19 { 20 case 0://平均值法 21 Result = ((r + g + b) / 3); 22 break; 23 case 1://最大值法 24 Result = r > g ? r : g; 25 Result = Result > b ? Result : b; 26 break; 27 case 2://加权平均值法 28 Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b)); 29 break; 30 } 31 bitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result)); 32 } 33 this.pictureBox1.Image = bitmap; 34 }