PictureBox with a ScrollBar - CodeProject


: 2


Today's topic is about how to add ScrollBar(s) into your PictureBox controller to make surfing it easier and better.

Why ?

Well, if you want to open a picture with a big size (example: 1920 x 1200) and your screen's resolution in ways smaller than that, then you will be obligated to resize or zoom it, other words: you will edit it.

Zooming or changing the size of a picture may distort it, so I suggest you to use scroll bars to make the view look better .

How to Add the Scrollbar

  1. Add a Panel Controller

  2. Click on it
  3. Go to the properties
  4. Select (AutoScroll) and change it from false to true

  5. Add a PictureBox (here like the picture shows)

  6. Go to the properties of the PictureBox, select SizeMode property and change it to (AutoSize)

That's it!

Using the Code

There isn't a lot of code in this topic, it is all related to the designer.

The only two code's methods I will post here are:

  1. Check if the picture exists or not.
  2. If it is does exist, then it will import it and show it into the PictureBox.
Check the Existence
if (System.IO.File.Exists(path)) // path is string which contains the path of the picture .
               //Import the picture
              //Error msg
Import the Picture
Pic.Image = Image.FromFile(path); // Pic is the name of the PictureBox controller

Final Code

if (System.IO.File.Exists(path))
               Pic.Image = Image.FromFile(path);
             //error msg


The code and the project are too simple, i just posted it to show a trick to add ScrollBar to the PictureBox controller .