How can I get scrollbars on Picturebox

C#WinformsScrollScrollbarPicturebox

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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;

hope this helps

Solution 2 - C#

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

Solution 3 - C#

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!

Solution 4 - C#

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);

Solution 5 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIchibannView Question on Stackoverflow
Solution 1 - C#BinilView Answer on Stackoverflow
Solution 2 - C#James KingView Answer on Stackoverflow
Solution 3 - C#smhikerView Answer on Stackoverflow
Solution 4 - C#ToyAuthor XView Answer on Stackoverflow
Solution 5 - C#SmithView Answer on Stackoverflow