winform用GDI+对图片进行灰度处理

2024-12-23 02:39:37
推荐回答(1个)
回答1:

主要用一些算法实现
private void button_Click(object sender, EventArgs e)
{
int width = this.pictureBox1.Image.Width;
int heigh = this.pictureBox1.Image.Height;

Bitmap oldbmp = (Bitmap)this.pictureBox1.Image;

Bitmap newbmp = new Bitmap(width, heigh);

Color pixel;

for (int x = 0; x < width; x++)
{
for (int y = 0; y < heigh; y++)
{
int r, g, b,result;
//获取原图中该像素点的三原色对象
pixel = oldbmp.GetPixel(x, y);
r = pixel.R;
g = pixel.G;
b = pixel.B;

result=(r+b+g)/3;

//设置新图片中该像素点的三原色
newbmp.SetPixel(x, y, Color.FromArgb(result, result, result));

}
}
this.pictureBox1.Image = newbmp;

}