Click here to Skip to main content
15,393,146 members
Articles / Programming Languages / C#
Posted 4 Apr 2006


33 bookmarked

Extended PictureBox control handles vast virtual screen

Rate me:
Please Sign up or sign in to vote.
3.74/5 (11 votes)
20 Feb 20073 min read
Extended PictureBox control handles vast virtual screen

Rev. 3 (DrawObj, RectObj, ImageObj)

Download - 70.9 KB

Download - 28.1 KB

Screenshot - Extended_PictureBox_img0702.png

Old ver.

Rev. 2 (RectObj-clickable)

First ver.


Have you ever been wanted to use a graphical view that has a large and vast screen ? Since the size of the PictureBox control in Windows.Forms namespace is limited to about 32k pixels, you cannot scatter graphical objects on vast space. But Graphics control can draw over the maximum size of the PictureBox control. And, it equips very useful method, 'TranslateTransform'. So, a small modification enables you to use vast 'virtual screen'.

In this article and source code, I demonstrate the ExtendedPictureBox that can handle virtual screen point easily for drawing and exchanging pointers by using a small app like a 'Google map'.

How does it work ?

In the demo application, Form has two group boxes, ExtendedPictureBox and scroll bars. In the virtual space, there are many rectangles (200,000 rects) that created on virtual screen with the size of (50,000 x 50,000). The ExtendedPictureBox control displays only the rectangles that intersects with displaying rectangle. By using scroll bars, you can see any rectangles in the space. Also, you can drag the screen. You may notice that the response during scrolling and dragging is fast.

Can I use on the VisualStudio ?

Yes, you can use this control in your own project and place the control on your Form on Visual Studio 2005.

  1. Download source file and upzip.
  2. Open project of ExtendedPictureBox, not TestForm.
  3. Build to compile the control as a 'Release version'
  4. Close Visual Studio.
  5. Finally, open your own project and add the reference to the ExtendedPictureBox.dll you built.

Extending PictureBox control

ExtendedPictureBox control has only 1 field, 'virtualPoint'. The virtualPoint is where the top-left point of the real screen is.

Sample imageSample image

private Point virtualPoint = new Point(0, 0);
public Point VirtualPoint 
    get { return virtualPoint; }
    set { virtualPoint = value; }

The virtual point is used for drawing at the OnExpandedPaint event.

protected void OnExpandedPaint(PaintEventArgs e)
    /// Set offset.
    e.Graphics.TranslateTransform(-(float)virtualPoint.X, -(float)virtualPoint.Y);

        this, new ExtendedPaintEventArgs(e.Graphics, e.ClipRectangle,
            new Rectangle(virtualPoint.X, virtualPoint.Y,
            this.ClientRectangle.Width, this.ClientRectangle.Height)));

You can see this event on the VisualStudio as follows. The event 'MouseMoveInVirtual' is another event that return the virtual point of the mouse cursor. If you want to use points on the real space, you can just use OnPaint or OnMouseMove.

Event selector on VisualStudio

In Demo Application

What the demo application does are

  1. Creation of instances of drawing rectangles at Form_Load.
  2. Painting of the only rectangles that intersects the displaying screen at virtual space.
  3. Handling of the event (Painting, MouseMove, ScrollBar, SearchButton)
  4. Searching pointed rectangles.

DrawObj class (added 2007.Feb.)

DrawObj is a base class for drawing. This class has a Rectangle instance for drawing. RectObj and ImageObj classes extend the DrawObj.

Screenshot - ClassDiagram1.png

RectObj class

This class is a very simple class for graphical object that contains geometry of its rectangle on the virtual space, Pen, Brush, Font and Name. All parameters are passed at the instantiation. Main method is only 'Draw' method.

In the Rev.2, drawing parameters (isSelected,selectionBrush and selectionPen) were added. If the 'IsSelected' property of RectObj is true, the selectionBrush and the selectionPen are used.

public void Draw(Graphics g)
    if (!isSelected)
        g.FillRectangle(drawBrush, objRect);
        g.FillRectangle(selectionBrush, objRect);

    if (!isSelected)
        g.DrawRectangle(drawPen, objRect);
        g.DrawRectangle(selectionPen, objRect);

    g.DrawString(text, drawFont, Brushes.Black, objRect.Location);

In the demo application,200,000 RectObj instances are created randomly and put in the List<RectObj> graphicsObjects collection. (Number of rectangles is defined by MAX_OBJECTS_COUNTS)

private void Form_Load(object sender, EventArgs e)
        Random rnd = new Random();
        for (int i = 0; i < MAX_OBJECTS_COUNTS; i++)
            // Create random rectangles.
            int left = rnd.Next(0, VIRTUAL_WIDTH);
            int top = rnd.Next(0, VIRTUAL_HEIGHT);
            int width = rnd.Next(RECTANGLE_MIN_WIDTH, RECTANGLE_MAX_WIDTH);
            int height = rnd.Next(RECTANGLE_MIN_HEIGHT,RECTANGLE_MAX_HEIGHT);
            Color col = Color.FromArgb((rnd.Next(int.MaxValue) & 0x7fffffff));

            /// RectObj with selectionPen and selectionBrush. --- Added 2006.4.16
            RectObj rect = new RectObj(new Rectangle(left, top, width, height), Pens.Black, new SolidBrush(col),
                selectionPen, selectionBrush, i.ToString(), LBL_FONT);
            rect.Tag = i;

Paint event

In order to draw only the displaying objects, all the RectObj are checked with the virtual points of the rectangle and the 'ClippingInVirtual'. The ClippingInVirtual parameter is translated to the point in the virtual space, you don't have to translate the points.

As the code shows, the drawing performance is not bad and the code is simple, I think.

private void extendedPictureBox_ExpandedOnPaint(object sender, ExtendedPaintEventArgs e)
    /// Draw only graphicsObjects that are in the clipping rectangle.
    for (int i = 0; i < graphicsObjects.Count; i++)
        if (graphicsObjects[i] != null && graphicsObjects[i].ObjRect.IntersectsWith(e.ClippingInVirtual))


Since the ExtendedPictureBox has simple structure with only one virtual point parameter and two event handlers, it is likely useful for other applications. I wish it helped your work or fun.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Japan Japan
Iquoba is a developer of software for bioscience and biotechnology at Symplus corp., who started with BASIC or Z80 assembler when he was twelve.

Comments and Discussions

GeneralA couple of questions [modified] Pin
xinye29-Jan-10 3:45
Memberxinye29-Jan-10 3:45 
GeneralGreat work. Pin
Xak.PC12-Nov-09 10:47
MemberXak.PC12-Nov-09 10:47 
QuestionHow to debug the "ExtentedPictureBox" project Pin
Swagatika8212-Mar-08 20:41
MemberSwagatika8212-Mar-08 20:41 
GeneralRe: How to debug the "ExtentedPictureBox" project Pin
Iquoba12-Mar-08 21:41
MemberIquoba12-Mar-08 21:41 
Questionabout OnPaint Pin
oguvenis24-Apr-07 15:21
Memberoguvenis24-Apr-07 15:21 
AnswerRe: about OnPaint Pin
Iquoba24-Apr-07 17:31
MemberIquoba24-Apr-07 17:31 
GeneralRe: about OnPaint Pin
oguvenis9-May-07 9:14
Memberoguvenis9-May-07 9:14 
GeneralAdding Picture Pin
oguvenis15-Feb-07 15:19
Memberoguvenis15-Feb-07 15:19 
GeneralRe: Adding Picture Pin
Iquoba20-Feb-07 5:45
MemberIquoba20-Feb-07 5:45 
GeneralRe: Adding Picture Pin
oguvenis20-Feb-07 6:56
Memberoguvenis20-Feb-07 6:56 
GeneralRe: Adding Picture Pin
Iquoba20-Feb-07 15:12
MemberIquoba20-Feb-07 15:12 
GeneralRe: Adding Picture Pin
oguvenis21-Feb-07 7:28
Memberoguvenis21-Feb-07 7:28 
Generalzooming capabilities Pin
osazuwa16-Apr-06 10:04
Memberosazuwa16-Apr-06 10:04 
GeneralRe: zooming capabilities Pin
Iquoba17-Apr-06 17:42
MemberIquoba17-Apr-06 17:42 
Generalclick event of rectobj Pin
osazuwa15-Apr-06 4:40
Memberosazuwa15-Apr-06 4:40 
GeneralRe: click event of rectobj Pin
Iquoba15-Apr-06 18:28
MemberIquoba15-Apr-06 18:28 
GeneralRe: click event of rectobj Pin
osazuwa16-Apr-06 9:56
Memberosazuwa16-Apr-06 9:56 
QuestionMissing source! Pin
Prasad Khandekar4-Apr-06 21:27
professionalPrasad Khandekar4-Apr-06 21:27 
AnswerRe: Missing source! Pin
Iquoba5-Apr-06 7:00
MemberIquoba5-Apr-06 7:00 
GeneralRe: Missing source! Pin
Iquoba5-Apr-06 16:31
MemberIquoba5-Apr-06 16:31 
AnswerRe: Missing source! Pin
Iquoba6-Apr-06 14:32
MemberIquoba6-Apr-06 14:32 

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.