简体   繁体   English

在使用SizeMode.Zoom,Winforms调整大小时,保持一个图片框在另一个图片框上的相对位置

[英]Keeping the relative position of a picturebox on another picturebox while resizing with SizeMode.Zoom, Winforms

Sample program for you to see whats going on: 示例程序供您查看发生了什么:

private void Form1_Load(object sender, EventArgs e)
    {
        int x = 0;
        int y = 0;
        this.Size = new Size(600, 600);
        PictureBox pb = new PictureBox();
        //use whatever somewhat bigger img
        pb.Image = Image.FromFile(@"");
        pb.Location = new Point(5, 5);
        pb.Size = new Size(500, 500);
        pb.SizeMode = PictureBoxSizeMode.StretchImage;
        this.Controls.Add(pb);

        PictureBox test30x30 = new PictureBox();
        //use a 30x30 img
        test30x30.Image = Image.FromFile(@"");
        test30x30.Size = new Size(30, 30);
        test30x30.BackColor = Color.Transparent;
        pb.Controls.Add(test30x30);

        pb.MouseClick += (ss, ee) =>
        {
            test30x30.Location = new Point(ee.X - test30x30.Image.Width / 2,     ee.Y - test30x30.Image.Height);
            x = (int)(((double)test30x30.Location.X + test30x30.Image.Width/2)     / (double)pb.Width * 1024);
            y = (int)(((double)test30x30.Location.Y + test30x30.Image.Height)     / (double)pb.Height * 1024);
        };

        this.Resize += (ss, ee) =>
        {
            pb.Size = new Size(this.Width - 100, this.Height - 100);
            test30x30.Location = new Point((int)((double)x / 1024 * (double)pb    .Width) - test30x30.Image.Width / 2, (int)((double)y / 1024 * (    double)pb.Height) - test30x30.Image.Height);
        };
    }

If you want to use my images: http://imgur.com/rfA0tpo,dhJX6Uc 如果要使用我的图像: http : //imgur.com/rfA0tpo,dhJX6Uc

First off i am using this type of resizing and not docking because my whole application requires it. 首先,我使用这种类型的调整大小而不是停靠,因为我的整个应用程序都需要它。 Anyways, this works fine for now the position of the second pictureBox stays where it should be if you resize the form. 无论如何,这现在可以正常工作,如果您调整窗体的大小,第二个pictureBox的位置将保持在应有的位置。 The problem is that StretchImage mode isnt really the best and i would want to use Zoom mode but then i would somehow need to get the zoomed size of the imageand not the picturebox and the actual offset of where the image is on the picturebox. 问题在于,StretchImage模式并不是真正的最佳选择,我想使用“缩放”模式,但随后我将以某种方式需要获取图像(而不是图片框)的缩放大小以及图片在图片框上的实际偏移量。 I wasnt able to figure this part out yet and was wondering if anybody had a similiar problem and solution. 我还无法弄清楚这部分,想知道是否有人有类似的问题和解决方案。

Since in Zoom mode the image size ratio is not changed, you can calculate the actual offset and size of image after piturebox is resized. 由于在“缩放”模式下图像尺寸比例未更改,因此可以在调整piturebox的尺寸后计算图像的实际偏移量和尺寸。

double imgWidth = 0.0;
double imgHeight = 0.0;
double imgOffsetX = 0.0;
double imgOffsetY = 0.0;
double dRatio = pb.Image.Height / (double)pb.Image.Width; //dRatio will not change during resizing

pb.MouseClick += (ss, ee) =>
{
    test30x30.Location = new Point(ee.X - test30x30.Image.Width / 2, ee.Y - test30x30.Image.Height);
    //x = ...
    //y = ...
};

this.Resize += (ss, ee) =>
{
    pb.Size = new Size(this.ClientSize.Width - 100, this.ClientSize.Height - 100);  
};

pb.SizeChanged += (ss, ee) =>
{
    //recalculate image size and offset
    if (pb.Height / pb.Width > dRatio) //case 1 
    {
        imgWidth = pb.Width;
        imgHeight = pb.Width * dRatio;

        imgOffsetX = 0.0;
        imgOffsetY = (pb.Height - imgHeight) / 2;
    }
    else //case 2 
    {
        imgHeight = pb.Height;
        imgWidth = pb.Height / dRatio;
        imgOffsetY = 0.0;
        imgOffsetX = (pb.Width - imgWidth) / 2;
    }

    //test30x30.Location = ...
}

变焦模式

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

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