Click here to Skip to main content
15,867,756 members
Articles / Desktop Programming / Windows Forms
Article

PictureBox with zooming and scrolling

Rate me:
Please Sign up or sign in to vote.
4.40/5 (28 votes)
11 Apr 2005CPOL2 min read 229.5K   21K   66   31
Article on creating a user defined PictureBox control.

Screenshot form PicturBoxCtrl testprogram

Introduction

In a past project, I designed a GUI with a PictureBox control on it. Because of the low screen resolution, I couldn't make the GUI big enough to show the whole picture. So I decided to develop my own scrollable PictureBox with the special highlight of zooming functionality. For that, I created a new VS project of type 'Windows control library'. In the source code, you can see the derived class.

How it works

When the mouse cursor enters the control, the cursor style changes and you are able to zoom in or out with the mouse-wheel. The principle of the zooming effect is to raise or to lower the inner PictureBox size by a fixed zooming factor. The scrollbars appear automatically when the inner PictureBox gets bigger than the control.

Using the control

The control resides in the namespace PictureBoxCtrl. There we have one public class PictureBox : System.Windows.Forms.UserControl.

The control provides two properties:

  • Picture - A string which represents the name of the picture. If an error occurs during load, the PictureBox shows a red cross. Supported file formats are: *.gif, *.tif, *.jpg, *.bmp.
  • Border - The same as the BorderStyle property in other controls.

Note: None of the properties have been added to the Appearance category in the property page of the control.

Bring the control to the Toolbox

  1. Compile the project PicturBoxCtrl.
  2. Go to the VS ToolBox.
  3. Create a tab (or use an existing one).
  4. Right click in the tab --> Add/Remove Items.

    Right-click in the toolbox

  5. Here you browse for the PictureBox.dll (in PictureBox\bin\Debug or PictureBox\bin\Release). Select it and press OK.

Browse for the .dll

Now you've rebound the control into the toolbox. From here, you can drop it to your form. The VS wizard will automatically add a reference for you to your project.

PicturBoxCtrl in the toolbox

That's it...

So, this is my first C# User Control. For my needs, the control is finished, maybe for yours too. If not, feel free to enhance the code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer www.rowa.de
Germany Germany
Working as software engineer since 2001. I started with c, c++ came up to c# and wpf today.

Comments and Discussions

 
QuestionError : Object reference not set to an instance of an object. Pin
Thilak Kumar10-Dec-12 20:41
Thilak Kumar10-Dec-12 20:41 
QuestionError : Object reference not set to an instance of an object. Pin
Thilak Kumar10-Dec-12 20:40
Thilak Kumar10-Dec-12 20:40 
QuestionError : Object reference not set to an instance of an object. Pin
Thilak Kumar10-Dec-12 20:40
Thilak Kumar10-Dec-12 20:40 
QuestionObject reference not set to an instance of an object. Pin
Thilak Kumar10-Dec-12 20:40
Thilak Kumar10-Dec-12 20:40 
Questionhow save image in picturebox? Pin
Jheiron17-Oct-11 5:29
Jheiron17-Oct-11 5:29 
AnswerRe: how save image in picturebox? Pin
Alexander Kloep17-Oct-11 20:41
Alexander Kloep17-Oct-11 20:41 
GeneralRe: how save image in picturebox? Pin
Jheiron18-Oct-11 3:17
Jheiron18-Oct-11 3:17 
GeneralRe: how save image in picturebox? Pin
Alexander Kloep18-Oct-11 20:27
Alexander Kloep18-Oct-11 20:27 
GeneralRe: how save image in picturebox? Pin
Jheiron19-Oct-11 3:16
Jheiron19-Oct-11 3:16 
GeneralMy vote of 5 Pin
Mr.YunJi31-Oct-10 7:04
Mr.YunJi31-Oct-10 7:04 
GeneralFile is being used by another process Pin
w, tim18-Jan-10 8:44
w, tim18-Jan-10 8:44 
GeneralRe: File is being used by another process Pin
Alexander Kloep18-Jan-10 19:36
Alexander Kloep18-Jan-10 19:36 
GeneralImage is too small Pin
huhuhujujuju9-Jan-10 3:13
huhuhujujuju9-Jan-10 3:13 
GeneralNice job Pin
Vuvirt26-Apr-08 8:45
Vuvirt26-Apr-08 8:45 
GeneralImage Zoomin and ZoomOut Pin
V K Gupta6-Feb-08 23:30
V K Gupta6-Feb-08 23:30 
GeneralImage Property Pin
SteveConradEllis7-Sep-06 1:17
SteveConradEllis7-Sep-06 1:17 
GeneralRe: Image Property Pin
Code Deamon19-Apr-07 1:33
Code Deamon19-Apr-07 1:33 
GeneralRe: Image Property Pin
alleyes10-Mar-09 5:28
professionalalleyes10-Mar-09 5:28 
GeneralDrag to scroll Pin
MrQBerrt30-Sep-05 10:01
MrQBerrt30-Sep-05 10:01 
GeneralRe: Drag to scroll Pin
Christopher Scholten10-Nov-05 18:06
professionalChristopher Scholten10-Nov-05 18:06 
NewsRe: Drag to scroll Pin
Christopher Scholten10-Nov-05 20:10
professionalChristopher Scholten10-Nov-05 20:10 
GeneralRe: Drag to scroll Pin
Seishin#15-Sep-08 11:29
Seishin#15-Sep-08 11:29 
GeneralSuggestion Pin
NinjaCross14-Sep-05 2:33
NinjaCross14-Sep-05 2:33 
GeneralRe: Suggestion Pin
Alexander Kloep14-Sep-05 19:43
Alexander Kloep14-Sep-05 19:43 
QuestionDisabled in ToolBox ? Pin
angel42019-Aug-05 8:40
angel42019-Aug-05 8:40 

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.