3

我正在尝试用 C# (WinForms) 创建一个应用程序,类似于这个iOS Question

我设法让它的一部分工作,我可以使用这个算法模糊图像

我还可以绘制一个选择矩形,我不知道模糊或传递矩形是否出错。我已附加文件,如下所示。 模糊效果

如图所示,模糊位于选择框之外。

我已经粘贴了下面的代码:

// Start Rectangle
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            // Determine the initial rectangle coordinates...
            RectStartPoint = e.Location;
            Invalidate();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
                return;
            Point tempEndPoint = e.Location;
            Rect.Location = new Point(
                Math.Min(RectStartPoint.X, tempEndPoint.X),
                Math.Min(RectStartPoint.Y, tempEndPoint.Y));
            Rect.Size = new Size(
                Math.Abs(RectStartPoint.X - tempEndPoint.X),
                Math.Abs(RectStartPoint.Y - tempEndPoint.Y));
            pictureBox1.Invalidate();
        }



        // Draw Area
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            // Draw the rectangle...
            if (pictureBox1.Image != null)
            {
                if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
                {                        
                    e.Graphics.DrawRectangle(selectionPen, Rect);
                }
            }
        }


        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            //Right now I am using right click as a call to blur
            if (e.Button == MouseButtons.Right)
            {
                if (Rect.Contains(e.Location))
                {                        
                    pictureBox1.Image = Blur(pictureBox1.Image, Rect, 5);
                    pictureBox1.Refresh();
                }
            }
        }

        private void blurPageToolStripMenuItem_Click(object sender, EventArgs e)
        {            
            FullRect = new Rectangle(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height);
            pictureBox1.Image = Blur(pictureBox1.Image, FullRect, 5);
        }

        private System.Drawing.Image Blur(System.Drawing.Image image, Rectangle rectangle, Int32 blurSize)
        {
           Bitmap blurred = new Bitmap(image);   //image.Width, image.Height);
            using (Graphics graphics = Graphics.FromImage(blurred))
            {
                // look at every pixel in the blur rectangle
                for (Int32 xx = rectangle.Left; xx < rectangle.Right; xx += blurSize)
                {
                    for (Int32 yy = rectangle.Top; yy < rectangle.Bottom; yy += blurSize)
                    {
                        Int32 avgR = 0, avgG = 0, avgB = 0;
                        Int32 blurPixelCount = 0;
                        Rectangle currentRect = new Rectangle(xx, yy, blurSize, blurSize);

                        // average the color of the red, green and blue for each pixel in the
                        // blur size while making sure you don't go outside the image bounds
                        for (Int32 x = currentRect.Left; (x < currentRect.Right && x < image.Width); x++)
                        {
                            for (Int32 y = currentRect.Top; (y < currentRect.Bottom && y < image.Height); y++)
                            {
                                Color pixel = blurred.GetPixel(x, y);

                                avgR += pixel.R;
                                avgG += pixel.G;
                                avgB += pixel.B;

                                blurPixelCount++;
                            }
                        }

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

                        // now that we know the average for the blur size, set each pixel to that color
                        graphics.FillRectangle(new SolidBrush(Color.FromArgb(avgR, avgG, avgB)), currentRect);                            
                    }
                }
                graphics.Flush();
            }
            return blurred;
        }       

我面临的另一个问题是,最初加载表单时,它以最小化模式启动,现在如果我使用选择(红色矩形),然后如果我最大化应用程序,图片的选定部分会有所不同。

提前感谢您的帮助/建议。如果有任何指向类似工具的链接,请分享,因为我可能错过了。谢谢

4

1 回答 1

1

您可能会遇到此问题,因为您的图像在 PictureBox 中被拉伸。您可以通过将SizeModePictureBox 的属性设置为Normal.

这是事件的顺序:

  1. 选择矩形被绘制。
  2. 确定选择的点/矩形。
  3. 从 PictureBox 中检索未拉伸的图像,并将其与计算出的矩形一起传递给 Blur 方法。
  4. 未拉伸的图像在矩形描述的区域上变得模糊。
  5. 现在模糊的未拉伸图像被分配给 PictureBox。
  6. PictureBox 根据其SizeMode设置将图像拉伸到 。

这会使图像在与您选择的位置不同的位置看起来模糊。

您拥有的代码查看选择矩形,并使用这些点对原始图像进行操作,而不是图像的拉伸版本。如果要模糊拉伸的图像,则需要先获取拉伸的图像,然后将模糊应用于该图像上选定的矩形。这是一个例子:

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        //Right now I am using right click as a call to blur
        if (e.Button == MouseButtons.Right)
        {
            if (Rect.Contains(e.Location))
            {
                pictureBox1.Image = Blur(getPictureBoxImage(), Rect, 5);
                pictureBox1.Refresh();
            }
        }
    }

    private Bitmap getPictureBoxImage()
    {
        Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        using (Graphics g = Graphics.FromImage(bmp))
        {
            g.DrawImage(pictureBox1.Image,
                new Rectangle(0, 0, bmp.Width, bmp.Height));
        }
        return bmp;
    }

检索拉伸图像的代码来自此答案:https ://stackoverflow.com/a/8702405/935052

于 2013-09-30T18:58:03.120 回答