C#模糊图像 [英] C# Blurring an Image

查看:322
本文介绍了C#模糊图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过遍历一个图像的所有像素然后使用原始图像中像素的颜色除以像素数来创建平均颜色来创建新的位图,从而使C#中的图像模糊.当我运行它时,什么也没发生.这是代码:

I am trying to blur an image in C# by going through all of the pixels of one image and then creating a new Bitmap with the color of the pixels in the original image divided by the pixel count to create an average color. When I run it, nothing happens. Here's the code:

private void blurToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Bitmap img = new Bitmap(pictureBox1.Image);
        Bitmap blurPic = new Bitmap(img.Width, img.Height);

        Int32 avgR = 0, avgG = 0, avgB = 0;
        Int32 blurPixelCount = 0;

        for (int y = 0; y < img.Height; y++)
        {
            for (int x = 0; x < img.Width; x++)
            {
                Color pixel = img.GetPixel(x, y);
                avgR += pixel.R;
                avgG += pixel.G;
                avgB += pixel.B;

                blurPixelCount++;
            }
        }

        avgR = avgR / blurPixelCount;
        avgG = avgG / blurPixelCount;
        avgB = avgB / blurPixelCount;

        for (int y = 0; y < img.Height; y++)
        {
            for (int x = 0; x < img.Width; x++)
            {
                blurPic.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB));
            }
        }

        img = blurPic;
    }

谢谢!

推荐答案

在方法末尾使用pictureBox1.Image = blurPic;.

这篇关于C#模糊图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆