Centered and scrolled PictureBox in WinForms

前端 未结 3 1556
孤街浪徒
孤街浪徒 2021-01-13 20:16

I\'m developing a WinForms application and can\'t figure out how to resolve an issue. I need to show an image in a Form. Because the image can be arbitrarily large, I need s

3条回答
  •  感动是毒
    2021-01-13 20:57

    For the PictureBox, set SizeMode = AutoSize, Anchor it Top, Left, and set its Location to 0, 0.

    Set Panel.AutSize to False and Panel.AutoScroll to True.

    When you set the PictureBox.Image property, it will auto-size to the size of the image. You can then use that size to set the panel's AutoScrollPosition property:

    public Image CapturedImage
    {
        set 
        { 
            pictureBoxCapturedImage.Image = value;
            panelCapturedImage.AutoScrollPosition = 
                new Point { 
                    X = (pictureBoxCapturedImage.Width - panelCapturedImage.Width) / 2, 
                    Y = (pictureBoxCapturedImage.Height - panelCapturedImage.Height) / 2 
                };
        }
    }
    

    If the image is smaller then then panel's size, it will remain in the upper left corner. If you want it centered within the panel, you'll have to add logic to set its Location appropriately.

提交回复
热议问题