2013-09-30 70 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

謝謝你的回答,我現在看到了問題。此外,這個小而有效的代碼就像一個魅力。 –