Click here to Skip to main content
13,762,666 members
Click here to Skip to main content
Add your own
alternative version

Stats

199.9K views
19K downloads
66 bookmarked
Posted 11 Apr 2005
Licenced CPOL

PictureBox with zooming and scrolling

, 11 Apr 2005
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Alexander Kloep
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.

You may also be interested in...

Comments and Discussions

 
QuestionError : Object reference not set to an instance of an object. Pin
Thilak Kumar10-Dec-12 21:41
memberThilak Kumar10-Dec-12 21:41 
QuestionError : Object reference not set to an instance of an object. Pin
Thilak Kumar10-Dec-12 21:40
memberThilak Kumar10-Dec-12 21:40 
QuestionError : Object reference not set to an instance of an object. Pin
Thilak Kumar10-Dec-12 21:40
memberThilak Kumar10-Dec-12 21:40 
QuestionObject reference not set to an instance of an object. Pin
Thilak Kumar10-Dec-12 21:40
memberThilak Kumar10-Dec-12 21:40 
Questionhow save image in picturebox? Pin
Jheiron17-Oct-11 6:29
memberJheiron17-Oct-11 6:29 
AnswerRe: how save image in picturebox? Pin
Alexander Kloep17-Oct-11 21:41
memberAlexander Kloep17-Oct-11 21:41 
GeneralRe: how save image in picturebox? Pin
Jheiron18-Oct-11 4:17
memberJheiron18-Oct-11 4:17 
GeneralRe: how save image in picturebox? Pin
Alexander Kloep18-Oct-11 21:27
memberAlexander Kloep18-Oct-11 21:27 
GeneralRe: how save image in picturebox? Pin
Jheiron19-Oct-11 4:16
memberJheiron19-Oct-11 4:16 
GeneralMy vote of 5 Pin
Mr.YunJi31-Oct-10 8:04
memberMr.YunJi31-Oct-10 8:04 
GeneralFile is being used by another process Pin
w, tim18-Jan-10 9:44
memberw, tim18-Jan-10 9:44 
GeneralRe: File is being used by another process Pin
Alexander Kloep18-Jan-10 20:36
memberAlexander Kloep18-Jan-10 20:36 
GeneralImage is too small Pin
huhuhujujuju9-Jan-10 4:13
memberhuhuhujujuju9-Jan-10 4:13 
GeneralNice job Pin
X3m26-Apr-08 9:45
memberX3m26-Apr-08 9:45 
GeneralImage Zoomin and ZoomOut Pin
V K Gupta7-Feb-08 0:30
memberV K Gupta7-Feb-08 0:30 
GeneralImage Property Pin
SteveConradEllis7-Sep-06 2:17
memberSteveConradEllis7-Sep-06 2:17 
GeneralRe: Image Property Pin
Code Deamon19-Apr-07 2:33
memberCode Deamon19-Apr-07 2:33 
GeneralRe: Image Property Pin
alleyes10-Mar-09 6:28
professionalalleyes10-Mar-09 6:28 
GeneralDrag to scroll PinPopular
MrQBerrt30-Sep-05 11:01
memberMrQBerrt30-Sep-05 11:01 
GeneralRe: Drag to scroll Pin
Christopher Scholten10-Nov-05 19:06
professionalChristopher Scholten10-Nov-05 19:06 
NewsRe: Drag to scroll Pin
Christopher Scholten10-Nov-05 21:10
professionalChristopher Scholten10-Nov-05 21:10 
One side effect of this is that when the mouse enters the control again after losing focus, and subsequently having focus re-set in the PicBox_MouseEnter event with PicBox.Focus();, the image reverts to having its top left corner aligned with the top left corner of the Outer Panel. To alleviate this, Add the line of code OuterPanel.AutoScrollPosition = scrollPosn; in the PicBox_MouseEnter event so that it reads as follows:

<br />
        private void PicBox_MouseEnter(object sender, EventArgs e)<br />
        {<br />
            if ( PicBox.Focused == false )<br />
            {<br />
                PicBox.Focus();<br />
                OuterPanel.AutoScrollPosition = scrollPosn;<br />
            }<br />
        }<br />


Unfortunately this does result in a slight flicker of the image when the mouse re-enters this area, however I haven't been able to get rid of this, even with calls to .SuspendLayout()/.ResumeLayout() calls on either PicBox or OuterPanel.
Rose | [Rose]
GeneralRe: Drag to scroll Pin
Seishin#15-Sep-08 12:29
memberSeishin#15-Sep-08 12:29 
GeneralSuggestion Pin
NinjaCross14-Sep-05 3:33
memberNinjaCross14-Sep-05 3:33 
GeneralRe: Suggestion Pin
Alexander Kloep14-Sep-05 20:43
memberAlexander Kloep14-Sep-05 20:43 
QuestionDisabled in ToolBox ? Pin
angel42019-Aug-05 9:40
memberangel42019-Aug-05 9: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181112.3 | Last Updated 12 Apr 2005
Article Copyright 2005 by Alexander Kloep
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid