65.9K
CodeProject is changing. Read more.
Home

PictureBox with a ScrollBar

starIconstarIconstarIconstarIconstarIcon

5.00/5 (5 votes)

Mar 29, 2015

CPOL

1 min read

viewsIcon

53074

downloadIcon

2047

Surf your PictureBox using scrolling bars

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

  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
            }
            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 .