Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#1.0 C#2.0 C#3.0 C# C#4.0 C#3.5 , +
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 19:36pm
Edited 24-Jun-12 19:43pm
v2
Rate this: bad
good
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(...))[^].
 
—SA
  Permalink  
v2
Comments
vlad781 at 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 at 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 at 25-Jun-12 1:58am
   
Good answer +5!
Sergey Alexandrovich Kryukov at 25-Jun-12 2:05am
   
Thank you, Prasad.
--SA
Rate this: bad
good
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[^]
  Permalink  
Comments
CHill60 at 10-Jun-14 11:46am
   
2 years ago the OP said they didn't want a link to an article
Rate this: bad
good
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.
  Permalink  
v3
Comments
vlad781 at 25-Jun-12 3:57am
   
And if I find one how exactly would I implement it into my program?
muhtarqong at 25-Jun-12 9:47am
   
Since they are third party you can reference the related DLL in your project.

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

  Print Answers RSS
0 OriginalGriff 428
1 Sergey Alexandrovich Kryukov 283
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Peter Leow 100
0 Sergey Alexandrovich Kryukov 6,465
1 OriginalGriff 5,926
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 10 Jun 2014
Copyright © CodeProject, 1999-2014
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