2014-01-19 74 views
0

這是pictureBox1的paint事件中的代碼。 我需要找到變量mImage的位置。如何獲取圖像位置?

if (null != mImage) 
{ 
    e.Graphics.DrawImage(mImage, theLocationOfImage); 
} 

mImage是圖像類型。 而不是theLocationOfImage我需要把mImage的位置。 這是我得到了mImage:

private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) 
{ 
    Bitmap bmp = new Bitmap(pictureBox1.Image); 
    Bitmap bmp1 = GetPartOfImageInRect(bmp, mRect); 
    CalculateNewSizeFactor(e.Delta); 
    Image img1 = ResizeImage(bmp1, 
     new Size((int)(bmp1.Width * currentfactor), 
      (int)(bmp1.Height * currentfactor))); 
    mImage = img1; 

    pictureBox1.Invalidate(); 
} 

mRect是一個矩形我畫過pictureBox1。

編輯

我這是怎麼繪製矩形:

private void DrawRectangle(Graphics e) 
     { 
      using (Pen pen = new Pen(Color.Red, 2)) 
      { 
       e.DrawRectangle(pen, mRect); 
      } 
     } 

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      mRect = new Rectangle(e.X, e.Y, 0, 0); 
      pictureBox1.Invalidate(); 
     } 

     private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) 
      { 
       mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top); 
       pictureBox1.Invalidate(); 
      } 
     } 

這是調整大小圖像的方法:

private Image ResizeImage(Image img, Size size) 
     { 
      return new Bitmap(img, size); 
     } 

這是pictureBox1漆事件:

if (null != mImage) 
      { 
       e.Graphics.DrawImage(mImage, theLocationOfImage); 
      } 
      DrawRectangle(e.Graphics); 

和最後的計算新的大小系數法:

private void CalculateNewSizeFactor(int delta) 
     { 
      if (delta > 0 && factor < 2.5) 
      { 
       factor *= increment; 
       currentfactor = factor; 
      } 
      else if (delta < 0 && factor > 0.25) 
      { 
       factor /= increment; 
       currentfactor = factor; 
      } 
     } 

我可以調整大小變焦縮小整個圖像,但我想放大了僅繪製矩形區域上方。

EDIT

忘了補充此方法:

private Bitmap GetPartOfImageInRect(Bitmap source, Rectangle rect) 
     { 
      return source.Clone(rect, source.PixelFormat); 
     } 
+0

由您決定位置應該在哪裏。您想做什麼?你想調整圖像的一部分並將其放在原始圖像中?請解釋! –

+0

奧利維爾是的,我在pictureBox1上繪製了一個矩形,這就是mRect變量,當我使用鼠標滾輪時,我希望只有圖像的部分重新調整像放大/縮小 – user3200169

+0

奧利維爾我剛更新我的問題添加了需要的方法和事件。 – user3200169

回答

0

當變焦是,要被放大的矩形的縱橫比將可能是從整個圖像的縱橫比不同的問題。因此你必須考慮兩種不同的情況。

// Calculate the size and position of the zoomed rectangle. 
double zoomFactorX = picturBox1.Width/mRect.Width; 
double zoomFactorY = picturBox1.Height/mRect.Height; 
Size newSize; 
Point newLocation; 
if (zoomFactorX < zoomFactorY) { // Fit image portion horizontally. 
    newSize = new Size(picturBox1.Width, (int)Math.Round(zoomFactorX * mRect.Height)); 

    // We have a top and a bottom padding. 
    newLocation = new Point(0, (picturBox1.Height - newSize.Height)/2); 
} else { // Fit image portion vertically. 
    newSize = new Size((int)Math.Round(zoomFactorY * mRect.Width), picturBox1.Height); 

    // We have a left and a right padding. 
    newLocation = new Point((picturBox1.Width - newSize.Width)/2, 0); 
} 
+0

奧利維爾,我忘了添加一個小方法,所以我現在添加到我的問題。它叫:GetPartOfImageInRect也許是重要的。 – user3200169

+0

奧利維爾在你的答案我把這個代碼在哪裏?在鼠標滾輪事件? – user3200169

+0

我只會使用鼠標事件來確定縮放矩形的大小和位置。創建新圖像並計算其大小和位置應該轉到Paint方法。我只在這裏向你展示原則;你可能不得不調整我的代碼以適應你現有的代碼。 –