【问题标题】:How do I get the Image location?如何获取图像位置?
【发布时间】:2014-01-19 15:48:39
【问题描述】:

这是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;
            }
        }

我可以调整放大整个图像的大小,但我只想放大绘制矩形的区域。

编辑

忘记添加这个方法了:

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

【问题讨论】:

  • 由您来定义位置的位置。你想让我做什么?是否要调整图像的一部分并将其置于原始图像的中心?请解释!
  • Olivier 是的,我在 pictureBox1 上画了一个矩形,这就是 mRect 变量,当我使用鼠标滚轮时,我希望只有图像的 retangle 部分像放大/缩小一样调整大小跨度>
  • Olivier 我刚刚更新了我的问题,添加了所需的方法和事件。
  • Oliver 是的,它在原始图像中居中,我在同一个矩形位置绘制了矩形。所以它会调整大小,只放大绘制矩形的区域。
  • 我这样做了:e.Graphics.DrawImage(mImage, mRect);在油漆事件中。但是当它调整大小时,它看起来很模糊。

标签: c# winforms


【解决方案1】:

缩放时的问题是要缩放的矩形的纵横比可能与整个图像的纵横比不同。因此,您必须考虑两种不同的情况。

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

【讨论】:

  • Olivier 我忘了添加一个小方法,所以我现在将它添加到我的问题中。它叫:GetPartOfImageInRect 也许它很重要。
  • Olivier 在你的回答中我把这段代码放在哪里?在鼠标滚轮事件中?
  • 我只会使用鼠标事件来确定缩放矩形的大小和位置。创建新图像并计算其大小和位置应该转到 Paint 方法。我在这里只是向您展示原理;您可能需要调整我的代码以适应您现有的代码。
猜你喜欢
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
相关资源
最近更新 更多