如何在C#

时间:2016-05-24 03:20:14

标签: c# winforms

我在C#中有这个简单的代码,基本上是根据Windows窗体中Trackbar的值对图像实现模糊过滤。

private void BarraBlur_Scroll_1(object sender, EventArgs e)
    {
        // I take the image inside picture box and use my own Effects class
        Bitmap image = new Bitmap(pictureBox1.Image);
        Effects foto = new Effects(image);

        switch (BarraBlur.Value)
        {
            case 0:

                pictureBox1.Image = imagens;  // This reestablishes the image to its original state
                break;
            case 1:
                // Each case reestablishes the image to its original state
                // and then applies the blur filter with a given depth
                pictureBox1.Image = imagens;
                pictureBox1.Image = foto.BlurEffect(1);
                break;
            case 2:
                pictureBox1.Image = imagens;
                pictureBox1.Image = foto.BlurEffect(2);
                break;
            case 3:
                pictureBox1.Image = imagens;
                pictureBox1.Image = foto.BlurEffect(3);
                break;
            case 4:
                pictureBox1.Image = imagens;
                pictureBox1.Image = foto.BlurEffect(4);
                break;
            case 5:
                pictureBox1.Image = imagens;
                pictureBox1.Image = foto.BlurEffect(5);
                break;
            default:
                break;
        }
   }

我想要的是,根据轨道栏的设置值,将模糊滤镜应用于图像。 BlurEffect方法采用 int 参数,以便以这样的深度值应用过滤器。

问题在于,例如,如果用户将轨迹栏设置在第二个位置,它可以正常工作,但是当它将其设置回第一个位置时,而不是将图像返回到其原始状态,然后应用深度为1的模糊滤镜,它将深度为1的模糊应用于深度为2的已模糊图像。

换句话说,我希望轨迹栏在每次刻度线向右移动时增加滤镜的深度,并在每次轨迹栏的刻度线向左移动时减小模糊的深度。

我已尝试使用switch case和if语句,但两者都没有用。

提前多多感谢。

这是带有BlurEffect方法的Effects类。

    class Effects
{
    private Bitmap imagen;

    // Constructor
    public Effects(Bitmap item)
    {
        imagen = item;
    }
public Bitmap BlurEffect(int depth)
    {
        for (int k = 0; k < depth; k++)
        {
            for (int i = 2; i < imagen.Width; i++)
            {
                for (int j = 2; j < imagen.Height; j++)
                {

                    try
                    {

                        Color antX1 = imagen.GetPixel(i - 1, j);
                        Color antX2 = imagen.GetPixel(i - 2, j);

                        Color desX1 = imagen.GetPixel(i + 1, j);
                        Color desX2 = imagen.GetPixel(i + 2, j);

                        Color antY1 = imagen.GetPixel(i, j - 1);
                        Color antY2 = imagen.GetPixel(i, j - 2);

                        Color desY1 = imagen.GetPixel(i, j + 1);
                        Color desY2 = imagen.GetPixel(i, j + 2);


                        int promR = (int)((antX1.R + antX2.R + desX1.R + desX2.R + antY1.R + antY2.R + desY1.R + desY2.R + imagen.GetPixel(i, j).R) / 9);
                        int promG = (int)((antX1.G + antX2.G + desX1.G + desX2.G + antY1.G + antY2.G + desY1.G + desY2.G + imagen.GetPixel(i, j).G) / 9);
                        int promB = (int)((antX1.B + antX2.B + desX1.B + desX2.B + antY1.B + antY2.B + desY1.B + desY2.B + imagen.GetPixel(i, j).B) / 9);


                        imagen.SetPixel(i, j, Color.FromArgb(promR, promG, promB));


                    }

                    catch (Exception) { }
                }
            }
        }

        return imagen;
    }
}

2 个答案:

答案 0 :(得分:0)

pictureBox1.Image = imagens;    
if (BarraBlur.Value == 0) return;
pictureBox1.Image = foto.BlurEffect(BarraBlur.Value);

上面替换了你的整个代码。您的问题是使用BlurEffect方法。出于这个原因,无论何时使用0都可以,因为您将图像返回到原始图像。您需要在BlurEffect方法的开头将图像重新加载到原始图像,而不是我猜测重复使用图片。

答案 1 :(得分:0)

首先,您不需要重复代码的大开关。您可以将代码缩减为:

if (BarraBlur.Value = 0)
{
    pictureBox1.Image = imagens;  // This reestablishes the image to its original state
}
else {
    pictureBox1.Image = foto.BlurEffect(BarraBlur.Value);
}

但是,这不会解决您的问题。我不知道类foto是什么,但它听起来就像它是Bitmap的包装器一样,BlurEffect()函数在之前对该位图应用模糊效果归还它。您的代码似乎是在假设BlurEffect返回位图的模糊副本的情况下编写的。

您可以修改代码,方法是在每次更改模糊时更改代码以构建新的foto

if (BarraBlur.Value = 0)
{
    pictureBox1.Image = imagens;  // This reestablishes the image to its original state
}
else {
    var blur = new Foto(imagens);   // or however the foto is constructed
    pictureBox1.Image = blur.BlurEffect(BarraBlur.Value);
}

由于您已表明您已编写/控制foto类(名为Effects),因此您还可以更改该类的工作方式(而不是执行上述操作)。

听起来像目前看起来像:

class Effects
{
    private Bitmap _image;

    public Effects(Bitmap image)
    {
        _image = image;
    }

    public Bitmap Blur(int amount)
    {
        // .. code to perform blur on _image ..
        return _image;
    }
}

你应该把它改成更像:

class Effects
{
    private Bitmap _image;

    public Effects(Bitmap image)
    {
        _image = image;
    }

    public Bitmap Blur(int amount)
    {
        var copy = _image.Clone();
        // .. code to perform blur on copy ..
        return copy;
    }
}