繁体   English   中英

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

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

示例程序供您查看发生了什么:

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

如果要使用我的图像: http : //imgur.com/rfA0tpo,dhJX6Uc

首先,我使用这种类型的调整大小而不是停靠,因为我的整个应用程序都需要它。 无论如何,这现在可以正常工作,如果您调整窗体的大小,第二个pictureBox的位置将保持在应有的位置。 问题在于,StretchImage模式并不是真正的最佳选择,我想使用“缩放”模式,但随后我将以某种方式需要获取图像(而不是图片框)的缩放大小以及图片在图片框上的实际偏移量。 我还无法弄清楚这部分,想知道是否有人有类似的问题和解决方案。

由于在“缩放”模式下图像尺寸比例未更改,因此可以在调整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