Search code examples
c#picturebox

How to make a PictureBox scrollable?


On my form i have a PictureBox inside Panel.
I set:

MyPanel.AutoScroll = true
MyPictureBox.SizeMode = AutoSize

After i add image into PictureBox:

MyPictureBox.Image = Image.FromFile(path);

But when i open form i don't see any scrollbars inside.
What can be wrong?


Solution

  • You have to probably set height and width of PictureBox and set AutoScroll property of Panel to true.

    Panel MyPanel = new Panel();
    PictureBox pictureBox1 = new PictureBox();
    
    Image image = Image.FromFile("image.png");
    
    pictureBox1.Image = image;
    pictureBox1.Height = image.Height;
    pictureBox1.Width = image.Width;
    
    MyPanel.Controls.Add(pictureBox1);
    MyPanel.AutoScroll = true;
    this.Controls.Add(MyPanel);