在图片框上重新定位矩形缩放

时间:2016-05-30 02:59:12

标签: c# visual-studio image-resizing rectangles

我在面板内部有一个pictureBox,允许我在mouseWheel事件上放大和缩小pictureBox。我现在面临的问题是,当我放大和缩小时,我无法根据新的图片框大小设置矩形的位置,正如我们在图像中看到的那样 - Resized Image, Zoom-OutResized Image, Zoom-In(由于声誉较低,无法上传更多照片)。现在我可以在puctureBox上绘制多个矩形,矩形存储在rectList中,rectList是RectangleList的变量,RectangleList是一个包含Rectangle的自定义类。 我提到How to make a rectangle move when PictureBox is resized来计算绘制的每个矩形的新位置,但没有成功。请帮我找到新的位置,因为我无法找到计算新位置的方法。

以下是MouseWheel事件的代码。

private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
    {
        if (e.Delta != 0)
        {
            if (e.Delta <= 0)
            {
                //set minimum size to zoom
                if (pictureBox1.Width < 350)
                    return;
            }
            else
            {
                //set maximum size to zoom
                if (pictureBox1.Width > 10000)
                    return;
            }

            int[] oldRectX = new int[rectList.Count];
            int[] oldRectY = new int[rectList.Count];
            int oldPictureBoxWidth = pictureBox1.Width;
            int oldPictureBoxHeight = pictureBox1.Height;

            for (int i = 0; i < rectList.Count; i++)
            {
                oldRectX[i] = rectList[i].rectangle.X;
                oldRectY[i] = rectList[i].rectangle.Y;
            }

            pictureBox1.Width += Convert.ToInt32(pictureBox1.Width * e.Delta / 1000);
            pictureBox1.Height += Convert.ToInt32(pictureBox1.Height * e.Delta / 1000);

            for (int i = 0; i < rectList.Count; i++)
            {
                rectList[i].rectangle.Width += Convert.ToInt32(rectList[i].rectangle.Width * e.Delta / 1000);
                rectList[i].rectangle.Height += Convert.ToInt32(rectList[i].rectangle.Height * e.Delta / 1000);

                rectList[i].rectangle.X = Convert.ToInt32(oldRectX[i] * (pictureBox1.Width / oldPictureBoxWidth));
                rectList[i].rectangle.Y = Convert.ToInt32(oldRectY[i] * (pictureBox1.Height / oldPictureBoxHeight));
                rectList[i].SetNewLocationOfSubRectList();
            }
            pictureBox1.Location = new Point((pictureBox1.Parent.ClientSize.Width / 2) - (pictureBox1.Width / 2), (pictureBox1.Parent.ClientSize.Height / 2) - (pictureBox1.Height / 2)); // Set the pictureBox1 to Center
            if (pictureBox1.Width > panel1.Width || pictureBox1.Height > panel1.Height)
            {
                Cursor = Cursors.Hand;
                mouseModeEnum = MouseMode.PANNING;
            }
            else
            {
                mouseModeEnum = MouseMode.NONE;
                Cursor = Cursors.Default;
            }
            pictureBox1.Invalidate();
        }
    }

我尝试计算其工作的How to make a rectangle move when PictureBox is resized中建议的位置百分比,但不太令人满意,因为当我将百分比转换为从double到int的位置时,它会截断一些小数值。如果有更好的方法,请告诉我。 我的新代码是

private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
    {
        if (e.Delta != 0)
        {
            if (e.Delta <= 0)
            {
                //set minimum size to zoom
                if (pictureBox1.Width < 350)
                    return;
            }
            else
            {
                //set maximum size to zoom
                if (pictureBox1.Width > 10000)
                    return;
            }

            double[] xLocPercent = new double[rectList.Count];
            double[] yLocPercent = new double[rectList.Count];
            double Width = pictureBox1.Width;
            double Height = pictureBox1.Height;

            for (int i = 0; i < rectList.Count; i++)
            {
                double X = rectList[i].rectangle.X;
                double Y = rectList[i].rectangle.Y;

                xLocPercent[i] = (X / Width) * 100;
                yLocPercent[i] = (Y / Height) * 100;
            }

            Width = pictureBox1.Width += Convert.ToInt32(pictureBox1.Width * e.Delta / 1000);
            Height = pictureBox1.Height += Convert.ToInt32(pictureBox1.Height * e.Delta / 1000);

            for (int i = 0; i < rectList.Count; i++)
            {
                rectList[i].rectangle.Width += Convert.ToInt32(rectList[i].rectangle.Width * e.Delta / 1000);
                rectList[i].rectangle.Height += Convert.ToInt32(rectList[i].rectangle.Height * e.Delta / 1000);
                rectList[i].rectangle.X = Convert.ToInt32((Width * xLocPercent[i]) / 100);
                rectList[i].rectangle.Y = Convert.ToInt32((Height * yLocPercent[i]) / 100);
                rectList[i].SetNewLocationOfSubRectList();
            }

            pictureBox1.Location = new Point((pictureBox1.Parent.ClientSize.Width / 2) - (pictureBox1.Width / 2), (pictureBox1.Parent.ClientSize.Height / 2) - (pictureBox1.Height / 2));

            if (pictureBox1.Width > panel1.Width || pictureBox1.Height > panel1.Height)
            {
                Cursor = Cursors.Hand;
                mouseModeEnum = MouseMode.PANNING;
            }
            else
            {
                mouseModeEnum = MouseMode.NONE;
                Cursor = Cursors.Default;
            }
            pictureBox1.Invalidate();
        }
    }

0 个答案:

没有答案