Click here to Skip to main content
15,884,237 members
Articles / Programming Languages / C#
Tip/Trick

PictureBox with a ScrollBar

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
28 Mar 2015CPOL1 min read 51K   2K   11   6
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
C#
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
C#
Pic.Image = Image.FromFile(path); // Pic is the name of the PictureBox controller

Final Code

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFurther notes if scroll bar is not showing Pin
Member 1512713424-Nov-22 23:00
Member 1512713424-Nov-22 23:00 
PraiseGood Job !! Pin
pauljayhoon13-Feb-22 20:53
pauljayhoon13-Feb-22 20:53 
QuestionNo scrollbars show up in vb.net Pin
pwarborg11-Jun-17 0:59
pwarborg11-Jun-17 0:59 
AnswerRe: No scrollbars show up in vb.net Pin
Johan Hakkesteegt14-Sep-18 3:05
Johan Hakkesteegt14-Sep-18 3:05 
QuestionCan't view the images Pin
DaveMathews28-Mar-15 23:30
DaveMathews28-Mar-15 23:30 
AnswerRe: Can't view the images Pin
Alaa Ben Fatma29-Mar-15 2:25
professionalAlaa Ben Fatma29-Mar-15 2:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.