Click here to Skip to main content
15,744,193 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I have a picturebox where I load an image, then I would like to zoom into it by pressing "ctrl"+ mouse wheel movements.

How can I perform this in C# .net?

Thank you!
Updated 24-Aug-19 21:50pm
Sergey Alexandrovich Kryukov 2-Aug-13 18:18pm    
It has nothing to do with PictureBox. Better don't use this control, it's better to do it by yourself. What's the problem?

1 solution

This is simple enough. There are two unrelated aspects you have to put together 1) handling of keyboard and mouse events and checking keyboard status, 2) transformation of graphics. You need to put them together.

First of all, you need to handle the event MouseWheel or override a virtual method OnMouseWheel for the control you need to respond to mouse wheel:[^],[^],[^].

Pay attention that the event arguments give you the information on the value scrolled by the wheel in the property Delta.

In the code of the handler, you also need to check if Ctrl is pressed. You may also need to check if some other modifier keys, such as Alt or Shift are not pressed. You don't need to check each key though. Instead, you override the virtual methods OnKeyDown and OnKeyUp or handle the events KeyDown and OnKeyUp which will respond to all key events. In each event for each key, the state of modifier keys may or may not be changed. The current state of such keys is passed to you in event arguments in the value of the property Modifiers:[^],[^].

Store this value on each event and check the last modifier state in your handler of the mouse wheel event. If Ctrl is pressed (and maybe other modifier keys are not), do the zoom.

Now, the actual zoom. It really depends on what your graphics is and how you render it. If you are trying to work with PictureBox, it could be the bitmap, which should not really be zoomed with enlargement: it would produce unacceptable quality, pixellation. You can only reduce the size with acceptable quality, or even enlarge, but only slightly. You still can do it, if your actual bitmap data is prepared for maximum possible size, so at all scales it could only be reduced in size.

However, I don't know, maybe you data is actually vector, and you just render it on a PictureBox by same weird reason (which is actually a usual big mistake of many beginners). Then you can get perfect graphics at any scale, only you should get rid of PictureBox, even if you actually use some bitmap. Instead, you should directly render your graphics on some control (including a Form), through overriding the virtual method OnPaint or handling the event Paint:[^],[^].

You rendering method should depend on some data you store in your class field(s), including the zoom factor. When you change zoom (or anything else), you call one of the Invalidate methods, to cause redrawing:[^],[^],[^],[^].

If this is not a bitmap, you don't have to re-calculate all the graphics using the zoom factor. You should better use the property System.Drawing.Graphics.Transform:[^],[^].

The instance of the class System.Drawing.Graphics will be passed to your paint handler in the event arguments.

If you want to understand why PictureBox is so bad for the purpose, please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

On GDI+ rendering and invalidation, see also my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
How to speed up my application?[^],
Drawing Lines between mdi child forms[^].


Please see the most recent answer providing more detail on zoom: How to know the mouse position for control inside UserControl[^].

Share this answer
Ron Beyer 2-Aug-13 20:34pm    
Well put and good references, +5.
Sergey Alexandrovich Kryukov 2-Aug-13 20:37pm    
Thank you, Ron.
Member 10415645 6-May-14 6:22am    
Thank you
Sergey Alexandrovich Kryukov 6-May-14 10:25am    
You are welcome.
Maciej Los 15-Dec-14 12:43pm    

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