Click here to Skip to main content
Click here to Skip to main content

C# Scrollable Picturebox Custom Control

, 9 Jun 2008
Rate this:
Please Sign up or sign in to vote.
This article discusses the construction of a custom control used to add a scrollable Picturebox control to a project.

Introduction

This article discusses the construction of a custom control used to add a scrollable Picturebox control to a project. There are different ways to scroll an image; it can be done using GDI+ for example, but the approach used in this demo is based upon what I feel is the easiest approach; that being to just scroll a full sized image contained within a panel control set to auto size. By building the control as a custom control, you can reuse in any application where you might need to provide such capability.

image001.jpg
Figure 1: Custom Control Loaded into a Test Application

Since the control is based upon the panel control, you will pick up all of the properties contained in the panel control which will make this simple control immediately more useful (for example, you will be able to dock the control into a form).

Getting Started

In order to get started, unzip the included project and open the solution in the Visual Studio 2008 environment. There are two projects contained in the solution. TestControl is the first project and it is Windows form application used to test the custom control. The second project (XPicbox) is class library containing a single custom control called XtendPicBox.cs. In the solution explorer, you should note these items:

image002.png
Figure 2: Solution Explorer

The Custom Control (XtendPicBox.cs)

The extended picture box custom control contains everything necessary to display an image in a scrollable container. The control itself is an extension of the panel control. Within the extended control, a picture box control is configured and added to the panel’s control collection. When the control is initialized, the picture box is set to the upper left corner of the control and its size mode is set to normal. Properties within the control are used to both limit the consumer’s access to the auto scroll property of the panel control (it has to be set to true in order for the image to scroll) and to allow the consumer to set the image file displayed in the inner picture box at both design and runtime.

The custom control class begins with the following library imports:

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

Following the imports, the namespace and class are defined. A constructor is added as are a collection of local member variables. The local member variables include the declaration of the inner Picturebox control, the path to the Picturebox image, and a Boolean value used to set auto scroll to true (for the extended panel control). The constructor is used to configure the Picturebox and to add the Picturebox control to the extended panel control.

namespace XPicbox
{

    /// <span class="code-SummaryComment"><summary></span>
    /// Extend the panel control to provide a
    /// scrollable picturebox control
    /// <span class="code-SummaryComment"></summary></span>
    public partial class XtendPicBox : Panel
    {

#region Local Member Variables

        PictureBox innerPicture = new PictureBox();
        private string mPictureFile = string.Empty;
        private bool mAutoScroll = true;

#endregion


#region Constructor


        /// <span class="code-SummaryComment"><summary></span>
        /// Constructor - set up the inner
        /// picture box so that is plants itself
        /// in the top left corner of the panel 
        /// and so that its size mode is always
        /// set to normal.  At normal, the picture
        /// will appear at full size.
        /// <span class="code-SummaryComment"></summary></span>

        public XtendPicBox()
        {
            InitializeComponent();

            // add the inner picture
            innerPicture.Top = 0;
            innerPicture.Left = 0;
            innerPicture.SizeMode = PictureBoxSizeMode.Normal;
            
            Controls.Add(innerPicture);
        }

#endregion

Next up are the custom control’s properties. The first property is used to set the image file to be displayed within the inner Picturebox control. Attributes are set to describe the purpose of the property when that property is used within the IDE; the attributes also specify that the property will be set using the file name editor; this will support opening a file browser window within the IDE at design time if the control consumer chooses to set the image file at design time. If the editor were not specified here, the control consumer would have to key in the path the image file.

The second property merely overrides the extended panel control’s auto scroll property. By overriding the property and setting up the Browsable attribute to false, the control consumer will be denied the ability to set the property using the property editor at design time. Since we always want the panel’s auto scroll property set to true, we block access to the property and set the value within the custom control’s code.

#region Properties


        /// <span class="code-SummaryComment"><summary></span>
        /// Allow control consumer to set the image
        /// used in the internal picture box through
        /// this public and browsable property -
        /// set the editor to the file name editor
        /// to make the control easier to use at
        /// design time (to provide an interface for
        /// browsing to and selecting the image file
        /// <span class="code-SummaryComment"></summary></span>
        [Category("Image File")]
        [Browsable(true)]
        [Description("Set path to image file.")]
        [Editor(typeof(System.Windows.Forms.Design.FileNameEditor),
          typeof(System.Drawing.Design.UITypeEditor))]
        public string PictureFile
        {
            get
            {
                return mPictureFile;
            }
            set
            {
                mPictureFile = value;

                if (!string.IsNullOrEmpty(mPictureFile))
                {
                    // set the image to the image file
                    innerPicture.Image = Image.FromFile(mPictureFile);

                    // resize the image to match the image file
                    innerPicture.Size = innerPicture.Image.Size;
                }
            }
        }

        /// <span class="code-SummaryComment"><summary></span>

        /// Override the autoscroll property
        /// and use the browsable attribute
        /// to hide it from control consumer -
        /// The property will always be set
        /// to true so that the picturebox will
        /// always scroll
        /// <span class="code-SummaryComment"></summary></span>
        [Browsable(false)]
        public override bool AutoScroll
        {
            get
            {
                return mAutoScroll;
            }
            set
            {
                mAutoScroll = value;
            }
        }

#endregion

That is all there is to building the custom control. Once compiled, you may use the control in any project where this sort of functionality is needed.

The Test Form (Form1.vb)

Looking at the test project, you will find a single form containing a menu with File->Open and File->Exit functions defined and you will find a single instance of the custom control. The custom control is docked full within the form. There is not a lot of code here to worry about; the file open method does demonstrate the ability to set the control’s image file at runtime so you might want to take a look at that. The entire body of code for the test form is as follows:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace TestControl
{

    /// <span class="code-SummaryComment"><summary></span>

    /// Demo application for scrollable
    /// picturebox control
    /// <span class="code-SummaryComment"></summary></span>
    public partial class Form1 : Form
    {

        /// <span class="code-SummaryComment"><summary></span>
        /// Default constructor
        /// <span class="code-SummaryComment"></summary></span>
        public Form1()
        {
            InitializeComponent();
        }


        /// <span class="code-SummaryComment"><summary></span>
        /// Open a new image file into the scrollable
        /// picture box control
        /// <span class="code-SummaryComment"></summary></span>

        /// <span class="code-SummaryComment"><param name="sender"></param></span>
        /// <span class="code-SummaryComment"><param name="e"></param></span>
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Title = "Open Image File";
            openFileDialog1.Filter = "Bitmap Files|*.bmp" +
                "|Enhanced Windows MetaFile|*.emf" +
                "|Exchangeable Image File|*.exif" +
                "|Gif Files|*.gif|Icons|*.ico|JPEG Files|*.jpg" +
                "|PNG Files|*.png|TIFF Files|*.tif|Windows MetaFile|*.wmf";

            openFileDialog1.FilterIndex = 6;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();

            if (openFileDialog1.FileName == "")
                return;

            // set the extended picturebox control's
            // image to the open file dialog's filename
            this.xtendPicBox1.PictureFile = openFileDialog1.FileName;
        }


        /// <span class="code-SummaryComment"><summary></span>

        /// Close the demo application
        /// <span class="code-SummaryComment"></summary></span>
        /// <span class="code-SummaryComment"><param name="sender"></param></span>
        /// <span class="code-SummaryComment"><param name="e"></param></span>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}

Summary

This solution provides a custom control which may be used to display image files in a scrollable container. The methods used within the control are nothing new or novel; the project does provide a simple example of how to construct a custom control, how to extend an existing control, and how to add design time support to a custom control. The same approaches to building this custom control may be applied to constructing other custom controls.

License

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

Share

About the Author

salysle
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
Questiondraw rectangle Pinmemberpninag22-Feb-12 20:25 
GeneralSmall addition to get smooth scrolling PinmemberCozumel994-Mar-11 17:01 
GeneralRe: Small addition to get smooth scrolling PinmemberJulius Adam1-Feb-12 0:30 
GeneralBest doesn't work Pinmemberkreont21-Dec-08 12:33 
GeneralThanks PinmemberAKG6-Nov-08 18:41 
QuestionWhat does this have to do with LINQ? PinmemberJohn Brett16-Jun-08 23:00 
General"...unzip the included project and open the solution..." PinmemberMember 16448489-Jun-08 10:26 
GeneralRe: "...unzip the included project and open the solution..." PinmemberMember 16448489-Jun-08 11:16 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 9 Jun 2008
Article Copyright 2008 by salysle
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid