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

Extended PictureBox control handles vast virtual screen

, 20 Feb 2007
Rate this:
Please Sign up or sign in to vote.
Extended PictureBox control handles vast virtual screen

Rev. 3 (DrawObj, RectObj, ImageObj)

Download Extended_PictureBox_src3.zip - 70.9 KB

Download Extended_PictureBox_demo3.zip - 28.1 KB

Screenshot - Extended_PictureBox_img0702.png

Old ver.

Rev. 2 (RectObj-clickable)

First ver.


Introduction

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);

    ExpandedOnPaint(
        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);
    else
        g.FillRectangle(selectionBrush, objRect);

    if (!isSelected)
        g.DrawRectangle(drawPen, objRect);
    else
        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;
            graphicsObjects.Add(rect);
        }
            ----

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))
            graphicsObjects[i].Draw(e.Graphics);
    }
}

Conclusion

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.

License

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

Share

About the Author

Iquoba
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] Pinmemberxinye29-Jan-10 4:45 
GeneralGreat work. PinmemberMember 474327612-Nov-09 11:47 
QuestionHow to debug the "ExtentedPictureBox" project PinmemberSwagatika8212-Mar-08 21:41 
GeneralRe: How to debug the "ExtentedPictureBox" project PinmemberIquoba12-Mar-08 22:41 
Questionabout OnPaint Pinmemberoguvenis24-Apr-07 16:21 
AnswerRe: about OnPaint PinmemberIquoba24-Apr-07 18:31 
GeneralRe: about OnPaint Pinmemberoguvenis9-May-07 10:14 
GeneralAdding Picture Pinmemberoguvenis15-Feb-07 16:19 
GeneralRe: Adding Picture PinmemberIquoba20-Feb-07 6:45 
GeneralRe: Adding Picture Pinmemberoguvenis20-Feb-07 7:56 
GeneralRe: Adding Picture PinmemberIquoba20-Feb-07 16:12 
GeneralRe: Adding Picture Pinmemberoguvenis21-Feb-07 8:28 
Generalzooming capabilities Pinmemberosazuwa16-Apr-06 11:04 
GeneralRe: zooming capabilities PinmemberIquoba17-Apr-06 18:42 
Generalclick event of rectobj Pinmemberosazuwa15-Apr-06 5:40 
GeneralRe: click event of rectobj PinmemberIquoba15-Apr-06 19:28 
GeneralRe: click event of rectobj Pinmemberosazuwa16-Apr-06 10:56 
QuestionMissing source! PinmemberPrasad Khandekar4-Apr-06 22:27 
AnswerRe: Missing source! PinmemberIquoba5-Apr-06 8:00 
GeneralRe: Missing source! PinmemberIquoba5-Apr-06 17:31 
AnswerRe: Missing source! PinmemberIquoba6-Apr-06 15:32 

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 | Terms of Use | Mobile
Web02 | 2.8.150302.1 | Last Updated 20 Feb 2007
Article Copyright 2006 by Iquoba
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid