Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

PictureBox with a ScrollBar

0.00/5 (No votes)
28 Mar 2015 1  
Surf your PictureBox using scrolling bars

Image 1

Introduction

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

    Image 2

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

    Image 3

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

    Image 4

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

    Image 5

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
            }
            else
            {
              //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);
           }
           else
           {
             //error msg
           }

Notice

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here