简体   繁体   English

如何获得图像位置?

[英]How do I get the Image location?

This is the code in the paint event of pictureBox1. 这是pictureBox1的paint事件中的代码。 I need to find the location of the variable mImage. 我需要找到变量mImage的位置。

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

mImage is Image type. mImage是图像类型。 Instead theLocationOfImage I need to put the mImage location. 相反,我需要放置mImage的位置。 This is how I got the 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 is a rectangle I draw over the pictureBox1. mRect是我在pictureBox1上绘制的矩形。

EDIT 编辑

This is how i draw the rectangle: 这是我绘制矩形的方式:

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

This is the resize image method: 这是调整大小图像方法:

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

And this is the pictureBox1 paint event: 这是pictureBox1绘画事件:

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

And last the calculate new size factor method: 最后计算新的尺寸系数方法:

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

I could resize zoom in out the whole image but i want to zoom in out only the area the rectangle is drawn over. 我可以调整放大整个图像的大小,但是我只想缩小绘制矩形的区域。

EDIT 编辑

Forgot to add this method: 忘记添加此方法:

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

The problem when zooming is that the aspect ratio of the rectangle to be zoomed will probably be different from the aspect ratio of the whole image. 缩放时的问题是要缩放的矩形的纵横比可能与整个图像的纵横比不同。 Therefore you must consider two different cases. 因此,您必须考虑两种不同的情况。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM