Click here to Skip to main content
15,885,143 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
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
Updated 24-Jun-12 19:43pm
v2

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(...))[^].

—SA
 
Share this answer
 
v2
Comments
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.
Sergey Alexandrovich Kryukov 25-Jun-12 2:05am    
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.
--SA
Prasad_Kulkarni 25-Jun-12 1:58am    
Good answer +5!
Sergey Alexandrovich Kryukov 25-Jun-12 2:05am    
Thank you, Prasad.
--SA
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.
 
Share this answer
 
v3
Comments
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.
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[^]
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900