Click here to Skip to main content
12,747,862 members (40,354 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0 C#3.5 Controls , +
On my form, I have a picture box that contains an image. This image, however, is too large for the box itself. I need a way where I can click and drag to pan the image, and use the scroll wheel to zoom in and out. I have searched through many posts but could not find a direct and efficient way of doing this. This is where I am right now:

I decided to make a panel where my picture box would be located, and set the autoscroll to true. This way I could just drag the picture box itself around. But, I am unsure of even how to do that. Thinking a bit I know I need to take the position of my mouse on click (on the picture box) and then move the picture box the same amount of pixels that I moved the mouse, after the mouse release event but how do I do all this? I am unsure of the syntax required to do this job. As for the zooming in and out, I am lost for sure.

All help appreciated. However replying to tell me to take a look at another article is not in my interest since I spent the past few hours doing so and it would seem as if I looked at every possible page.

Thank you.
Posted 24-Jun-12 20:36pm
Updated 24-Jun-12 20:43pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The solution is simple: stop abusing PictireBox. This simple control class which could help you in simplest cases, not helping you here, only creates unwanted intermediate layer to eat up extra resources and waste extra development time giving nothing in return, compared to using a custom control class derived from System.Windows.Forms.Control.

Please see my past answers for further detail:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

See also:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

vlad781 25-Jun-12 1:55am
If I should not use picture box, then what should I use? I can't just draw the image to the form, not in my case anyway.
Prasad_Kulkarni 25-Jun-12 1:58am
Good answer +5!
I clearly answered - a custom control, which is way easier. You actually can draw a image on the form, why not? A form is a control. But in most cases, you should not. Read my past answers I referenced about. Only if something is not clear, please ask follow-up questions.
Thank you, Prasad.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Another solution is:
Make your custom control as SA suggested. If you do not have time to do it, please go to Google and search for "free imaging sdk".

You can find several free imaging SDKs including ThumbnailViewer, ImageViewer and DocumentViewer. This is convinient.
You can also find classes of "Panning" and "Magnifier (MagnifyingGlass)" for picture box.
vlad781 25-Jun-12 3:57am
And if I find one how exactly would I implement it into my program?
muhtarqong 25-Jun-12 9:47am
Since they are third party you can reference the related DLL in your project.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I did a little hunting around for a simple solution for you and came across this solution so I wanted to share it with you. This should solve your problem and it is a pretty easy implementation.

I hope this helps.

A Simple Panning PictureBox[^]
CHill60 10-Jun-14 11:46am
2 years ago the OP said they didn't want a link to an article

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170215.1 | Last Updated 10 Jun 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100