65.9K
CodeProject is changing. Read more.
Home

PictureBox with zooming and scrolling

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.40/5 (27 votes)

Apr 12, 2005

CPOL

2 min read

viewsIcon

234562

downloadIcon

21131

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.