2013-09-30 63 views
1

我试图创建在C#(的WinForms)应用程序的选定部分,与此类似iOS Question模糊图像

我设法得到它的工作的一部分的东西,我可以用这个algorithm

造成图像模糊

此外,我能够绘制一个选择矩形,我不知道如果我模糊或传递矩形错误。我已经附加了如下所示的文件。 Blurring Effect

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

我已经粘贴了以下代码:

// 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; 
     }  

我面临的另一个问题是,当表单最初加载时,它开始在最小化模式时,如果我现在使用选择(红色矩形),并那么如果我最大化应用程序,图片的选定部分是不同的。

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

+0

不知道,但'X

+0

谢谢,我会试试看。 –

+0

谢谢,我试过了,但没有奏效。该应用程序现在挂起||代替 &&。 –

回答

1

您可能遇到此问题,因为您的图像在PictureBox中拉伸。您可以通过将PictureBox的SizeMode属性设置为Normal来验证这是问题。

这是事件发生的顺序:

  1. 选择绘制矩形。
  2. 确定选择的点/矩形。
  3. 从图片框中检索未拉伸的图像,并将其传递给计算出的矩形的模糊方法。
  4. 未拉伸的图像在矩形描述的区域上模糊。
  5. 将未拉伸的图像(现在变模糊)分配给PictureBox。
  6. 根据其设置,图片框会将图像拉伸至其外观为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

+0

谢谢你的回答,我现在看到了问题。此外,这个小而有效的代码就像一个魅力。 –