【问题标题】:How to resize a picturebox without changing its middle coordinate如何在不改变中间坐标的情况下调整图片框的大小
【发布时间】:2017-06-21 04:50:59
【问题描述】:

不幸的是,调整大小时图片框跳到了下角。 我读到我需要用 Docker 和 Anchor 设置一些东西,但我不知道怎么做。

【问题讨论】:

    标签: c# .net windows-forms-designer


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-27
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 2018-11-02
      • 1970-01-01
      相关资源
      最近更新 更多