Click here to Skip to main content
Licence CPOL
First Posted 31 May 2008
Views 34,066
Downloads 1,476
Bookmarked 34 times

A Zoomable and Scrollable PictureBox

By | 31 May 2008 | Article
An ImagePanel component created to replace the PictureBox.

Introduction

A big question in the Windows Forms world is "How can I zoom and scroll the PictureBox?" The answer is to use the ImagePanel, which enables you to simply zoom and pan an image.

I use Microsoft Visual C# 2005 Express Edition to build it. It's easy and simple. You can start a new project and add a user control. In the Design window, you drag a vertical scrollbar and a horizontal scrollbar to the user control, then go to the code window and add the code as in the source file, ImagePanel.cs. That's all, a zoomable and scrollable PictureBox replacement has been built and you can use it now.

Zooming is done by a Matrix object:

Matrix mx=new Matrix(); // create an identity matrix
mx.Scale(zoom,zoom); // zoom image

Panning is done by the values returned by the scrollbars. We tell the scrollbar the image size, which is its real size multiplied by the zoom factor:

if ((canvasSize.Width * zoom - viewRectWidth) > 0)
{
    this.hScrollBar1.Maximum =(int)( canvasSize.Width * zoom) - viewRectWidth;
}
if ((canvasSize.Height * zoom - viewRectHeight) > 0)
{
    this.vScrollBar1.Maximum = (int)(canvasSize.Height * zoom) - viewRectHeight;
}

So, the correct position within the real image is the scrollbar values divided by the zoom factor:

Point pt=new Point((int)(hScrollBar1.Value/zoom),(int)(vScrollBar1.Value/zoom));

To display the image on the center of this control, the Matrix object is used again:

mx.Translate(viewRectWidth/2.0f,viewRectHeight/2.0f, MatrixOrder.Append);

This component has a good feature: during image zoom out, only a portion of the image is drawn. It saves a lot of computer resources:

if (canvasSize.Width * zoom < viewRectWidth && 
            canvasSize.Height * zoom < viewRectHeight)
    srcRect = new Rectangle(0, 0, canvasSize.Width, canvasSize.Height);
    // view all image
else srcRect = new Rectangle(pt, new Size((int)(viewRectWidth / zoom), 
                            (int)(viewRectHeight / zoom)));
// view a portion of image

To test this little but strong control, I put it on a form and added a trackbar to provide the zoom factor. Double click the ImagePanel to load a picture. Try it, how's it?

Thanks.

License

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

About the Author

YLS CS



Unknown

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey22:53 1 Mar '12  
GeneralExactly what I was looking for PinmemberEpikYummeh15:36 29 Nov '11  
Generalthanks Pinmemberasugix4:01 18 Jul '10  
GeneralGreat work! Pinmembersintesia6:56 25 Mar '10  
GeneralA Little Memory Used More Than Requared PinmemberMember 278444917:21 25 Mar '09  
QuestionHow can i transfer this to Windows Mobile 6? Pinmemberwomisa11:45 1 Mar '09  
Questionimage is not kept centered when zoom Pinmemberdvisions4:17 20 Sep '08  
AnswerRe: image is not kept centered when zoom Pinmemberrichardn669:28 17 Feb '10  
GeneralRe: image is not kept centered when zoom Pinmemberflacolarry13:41 19 Feb '10  
QuestionHow about to add croping, resizing? PinmemberDemaker1:20 2 Jul '08  
Generalmaybe simplified version PinmemberHuanacaraz21:38 2 Jun '08  
AnswerRe: maybe simplified version PinmemberYLS CS4:33 3 Jun '08  
Thank you very much for your suggestion.
 
I tried AutoScroll before. I wanted to put the image on the container center when image zoomed in. But I really didn't know how the image location change while zooming. I wonder only setting the AutoScrollMinSize is not enough. I will try again. Thanks.

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 31 May 2008
Article Copyright 2008 by YLS CS
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid