77

I have PictureBox picture.

I use:

picture.Size = bmp.Size;
picture.Image = bmp;

Let's say there are two integers maxWidth and maxHeigth.
I want to add vertical/horizontal scrollbar to picture when its size exceeds maxWidth and/or maxHeight. How can I do that?

Karim
  • 6,113
  • 18
  • 58
  • 83
Ichibann
  • 4,371
  • 9
  • 32
  • 39

5 Answers5

152

You can easily do it with a Panel Control

Insert a panel to your form, say panel1 and set

panel1.AutoScroll = true;

insert a PictureBox to the Panel, say picture and set

picture.SizeMode = PictureBoxSizeMode.AutoSize;

and set the Image

picture.Image = bmp;
starball
  • 20,030
  • 7
  • 43
  • 238
Binil
  • 6,445
  • 3
  • 30
  • 40
  • 3
    This is a nice answer because if you set the panel to be anchored to the form, the panel will expand as the form expands, showing and hiding the scrollbars as necessary. – James King Jan 17 '11 at 05:34
  • 20
    I would add to binil's answer, the following: the picturebox has to have the anchor not set to right or bottom. Setting the anchor to right prevents the display of the horizontal scrollbar. Setting it to bottom prevents the display of the vertical scrollbar. – CristisS Apr 09 '13 at 11:53
  • 19
    Mike, verify that pictureBox dock should be set to None. I had mine set to Fill and did not see any scrollbars but when I changed it to none the scrollbars appeared. – morpheus Mar 24 '15 at 22:53
  • 2
    I've the same problem, the pictureBox dock is set to None but If I set the SizeMode to AutoSize no scrollbars are shown – Martina May 11 '15 at 16:38
  • Yep same issue here, you cannot set SizeMode to AutoSize in the later versions. Setting it to Normal works as expected. – kory May 23 '16 at 18:49
  • I also set the panel1's AutoSize to false because it became as big as my screen. – Daniel Katz Jul 19 '16 at 22:19
  • Resuming ('cause i've done a lot of experiments), and this worked for me: Panel: Autosrcoll: true, Dock: fill Picturebox: Dock: none; Anchor: Top, Left; SizeMode: AutoSize – Angelo Bernardi Feb 05 '20 at 19:29
4

I got it to work by also putting a picturebox inside a panel control, I set the Panel's AutoScroll property to true, but I also set the Panel's Autosize property to True, and the Panel's Dock property to Fill (that way when the user resizes the form - so will the Panel). For the Picturebox, I set it's Dock property to None, and the SizeMode to Autosize (so it resizes also when the Panel and form Resizes. It worked like a charm, the Picturebox has Scrollbars and when the user resizes the form - everything is still placed correctly!

smhiker
  • 41
  • 4
  • Thanks, the only way it worked for me (VS2017) was PictureBox: Dock = None and SizeMode = AutoSize. Thanks again! – RickC May 12 '20 at 21:18
4

Here's a project where a guy built an ImagePanel user control that you can drop onto a form; it gives you scrollbars and zoom capability.

http://www.codeproject.com/KB/graphics/YLScsImagePanel.aspx

James King
  • 6,233
  • 5
  • 42
  • 63
2

It works to me.

PictureBox picture = new PictureBox();
picture.Image=Image.FromFile("image.bmp");
picture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
Panel panel = new Panel();
panel.Size=new Size(800,600);
panel.Location=new Point(0,0);
panel.AutoScroll=true;
panel.Controls.Add(picture);
this.Controls.Add(panel);
ToyAuthor X
  • 315
  • 4
  • 5
1

Another suggestion is to put the picturebox inside a FlowlayoutPanel .

Set the Auto scroll of the FlowlayoutPanel to true and set the picture size mode to normal

Using a FlowlayoutPanel makes sure the image is always at 0,0 in the panel

Smith
  • 5,765
  • 17
  • 102
  • 161