2017-06-20 68 views

回答

0

您的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); 
} 
相关问题