2010-04-02 41 views
3

我正在嘗試使用鼠標的位置來計算縮放圖像的縮放因子。基本上,你越遠離圖像中心,它越大;而越接近你得到的中心,它越小。到目前爲止我有一些代碼,但它的行爲真的很奇怪,我完全沒有更多的想法。首先我會告訴你,我試圖做的一件事是平均5距離以獲得更平滑的調整大小的動畫。這是我的代碼:在WinForms應用程序中使用鼠標縮放圖像?

private void pictureBoxScale_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (rotateScaleMode && isDraggingToScale) 
    { 
     // For Scaling    
     int sourceWidth = pictureBox1.Image.Width; 
     int sourceHeight = pictureBox1.Image.Height; 
     float dCurrCent = 0; // distance between the current mouse pos and the center of the image 
     float dPrevCent = 0; // distance between the previous mouse pos and the center of the image 

     System.Drawing.Point imgCenter = new System.Drawing.Point(); 

     imgCenter.X = pictureBox1.Location.X + (sourceWidth/2); 
     imgCenter.Y = pictureBox1.Location.Y + (sourceHeight/2); 

     // Calculating the distance between the current mouse location and the center of the image 
     dCurrCent = (float)Math.Sqrt(Math.Pow(e.X - imgCenter.X, 2) + Math.Pow(e.Y - imgCenter.Y, 2)); 

     // Calculating the distance between the previous mouse location and the center of the image 
     dPrevCent = (float)Math.Sqrt(Math.Pow(prevMouseLoc.X - imgCenter.X, 2) + Math.Pow(prevMouseLoc.Y - imgCenter.Y, 2)); 

     if (smoothScaleCount < 5) 
     { 
      dCurrCentSmooth[smoothScaleCount] = dCurrCent; 
      dPrevCentSmooth[smoothScaleCount] = dPrevCent; 
     } 


     if (smoothScaleCount == 4) 
     { 
      float currCentSum = 0; 
      float prevCentSum = 0; 
      for (int i = 0; i < 4; i++) 
      { 
       currCentSum += dCurrCentSmooth[i]; 
      } 
      for (int i = 0; i < 4; i++) 
      { 
       prevCentSum += dPrevCentSmooth[i]; 
      } 

      float scaleAvg = (currCentSum/5)/(prevCentSum/5); 


      int destWidth = (int)(sourceWidth * scaleAvg); 
      int destHeight = (int)(sourceHeight * scaleAvg); 

      // If statement is for limiting the size of the image 
      if (destWidth > (currentRotatedImage.Width/2) && destWidth < (currentRotatedImage.Width * 3) && destHeight > (currentRotatedImage.Height/2) && destWidth < (currentRotatedImage.Width * 3)) 
      { 
       AForge.Imaging.Filters.ResizeBilinear resizeFilter = new AForge.Imaging.Filters.ResizeBilinear(destWidth, destHeight); 
       pictureBox1.Image = resizeFilter.Apply((Bitmap)currentRotatedImage); 
       pictureBox1.Size = pictureBox1.Image.Size; 
       pictureBox1.Refresh(); 
      } 

      smoothScaleCount = -1; 
     } 
     prevMouseLoc = e.Location; 
     currentScaledImage = pictureBox1.Image; 
     smoothScaleCount++; 

    } 
} 

編輯:感謝Ben Voigt和Ray現在一切正常。唯一錯誤的是,用我做這件事的方式,圖像不能保持它的比例;但我會在稍後解決。下面是我對那些想知道誰:

private void pictureBoxScale_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (rotateScaleMode && isDraggingToScale) 
     { 
      // For Scaling    
      int sourceWidth = pictureBox1.Image.Width; 
      int sourceHeight = pictureBox1.Image.Height; 
      int scale = e.X + p0.X; //p0 is the location of the mouse when the button first came down 
      int destWidth = (int)(sourceWidth + (scale/10)); //I divide it by 10 to make it slower 
      int destHeight = (int)(sourceHeight + (scale/10)); 

      if (destWidth > 20 && destWidth < 1000 && destHeight > 20 && destWidth < 1000) 
      { 
       AForge.Imaging.Filters.ResizeBilinear resizeFilter = new AForge.Imaging.Filters.ResizeBilinear(destWidth, destHeight); 
       pictureBox1.Image = resizeFilter.Apply((Bitmap)currentRotatedImage); 
       pictureBox1.Size = pictureBox1.Image.Size; 
       pictureBox1.Refresh(); 
      } 
      currentScaledImage = pictureBox1.Image; // This is only so I can rotate the scaled image in another part of my program 

     } 
    } 
+1

那麼你的問題是什麼? – Aaronaught 2010-04-02 04:07:06

+0

@Aaronaught:「爲什麼這表現很奇怪?」 – 2010-04-02 04:11:02

+1

我會建議使用調試器進行單元測試和/或單步調試。我也會嘗試沒有「平滑縮放」,因爲有些部分看起來很可疑。 – 2010-04-02 04:12:59

回答

1

如果使用圖像的中心,則縮放比例將不平滑。相反,使用最初的鼠標點(稱爲p0)。而且,不是使用從該點到當前拖動點(e)的距離,而是沿着一個軸取差值(例如,exp(e.Y - p0.Y))。

+0

我不確定你爲什麼建議用exp來計算infinity-norm。你的意思是最大(abs(x1-x2),abs(y1-y2))? – 2010-04-02 15:12:08

+0

對我有意義,我喜歡。 – Gaax 2010-04-02 19:58:04

+0

@Ben。你不想要一個規範。你需要+/-值。 exp只是從+/-值中獲得縮放值的一些方法。 – Ray 2010-04-02 20:40:01

1

在我看來(從scaleAvg計算)就像你重新縮放已縮放後的圖像。這是一個非常糟糕的想法,因爲縮放是有損的,而且錯誤會累積起來。相反,請保留一份清晰的原始圖像,並將原稿直接縮放至當前尺寸。另外,我建議使用不同的標準,也許是曼哈頓距離,而不是當前的笛卡爾距離,這是一個兩個標準。

如果您確實繼續使用雙標準,請考慮刪除Math.Pow調用。它們可能只是整體比例複雜度的一小部分,它並不重要,但乘以其自身的速度應比Math.Pow快於數字的平方。

相關問題