2014-01-19 75 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方法。我只在这里向你展示原则;你可能不得不调整我的代码以适应你现有的代码。 –