0
不幸的是,调整大小期间的图片框跳到了下角。 我读到我需要设置Docker和Anchor的东西,但我不知道如何。如何在不改变其中间坐标的情况下调整图片框的大小
不幸的是,调整大小期间的图片框跳到了下角。 我读到我需要设置Docker和Anchor的东西,但我不知道如何。如何在不改变其中间坐标的情况下调整图片框的大小
您的Location
(x,y)指的是控件的左上角。调整PictureBox的大小时,除非您还更改位置,否则只有右下角会移动。
您是否更改了代码的大小?如果是这样,你可以使用一个辅助方法来做到这一点。如果图像变大,则图像需要左移宽度的一半,如果宽度变得更小(相同逻辑适用于高度),则需要向右移动宽度的一半。
private void ChangePictureBoxSize(int newWidth, int newHeight)
{
// these will be negative if picturebox is getting bigger
int changeInWidth = pictureBox1.Width - newWidth;
int changeInHeight = pictureBox1.Height - newHeight;
// will shift left and up if picturebox is getting bigger
int newX = pictureBox1.Location.X + (changeInWidth/2);
int newY = pictureBox1.Location.Y + (changeInHeight/2);
pictureBox1.Location = new Point(newX, newY);
pictureBox1.Size = new Size(newWidth, newHeight);
}