Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
I have UserControl in my Form
inside the UserControl there is a PictureBox

in the Form i control the PictureBox size to zoom it, that make the UserControl scroll

Now i want a way to know the Mouse position for PictureBox or for the image inside it

my app is windows form


Note: if the Scenario isn't clear, please ask me
Posted
Comments
BillWoodruff 15-Dec-14 21:42pm    
Why doesn't using the 'MouseMove Event of the PictureBox to access the current mouse location in PictureBox co-ordinates meet your needs ? Or using the 'MousePosition operator to get the mouse's current position in screen co-ordinates ?

1 solution

Usually, you need to know mouse coordinates only when you handle mouse events, but then these coordinates are given to you through the event arguments parameter of your handler. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventhandler%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs%28v=vs.110%29.aspx[^].

System.Windows.Forms.MouseEventArgs.X and Y represent the coordinates in the control's coordinate system.

By the way, you can always do coordinate transform from screen coordinates to the coordinate system of your control and back:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoclient(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoscreen(v=vs.110).aspx[^].

This static method gives you mouse position is screen coordinates at any time, regardless of your event handling: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseposition(v=vs.110).aspx[^].

That's all on the topic of mouse coordinates.

But your major problem is different. You should not use PictureBox; it won't help you. Even though you could solve the problem with PictureBox, it makes no practical sense at all, would only waste your development effort and some extra resources. I'll explain what to do instead. Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

And these are my past answers explaining graphics rendering:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
and this one is related specifically to zoom: Zoom image in C# .net mouse wheel[^].

Note that zooming means re-samling of your image, if you use a pixel graphics. Alternatively, you can do it using one of the methods System.Drawing.Graphics.DrawImage which accept the parameter specifying the target rectangle, so it will re-sample the image when the new image size is different (take care to preserve the aspect ratio): http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx[^].

Before all such operations, don't forget to set appropriate re-sampling quality using this property:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.interpolationmode%28v=vs.110%29.aspx[^].

You get the best re-sampling (interpolation) quality when you use System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic. Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode%28v=vs.110%29.aspx[^].

Now, this is a big warning for you: you can easily re-sample the image down, but if you want do increase the size, you can do it only slightly, otherwise your final image quality would be unacceptable.

—SA
 
Share this answer
 
v3
Comments
Maciej Los 15-Dec-14 12:44pm    
5ed!
Sergey Alexandrovich Kryukov 15-Dec-14 14:27pm    
Thank you, Maciej.
—SA
M. Daban 16-Dec-14 9:59am    
Perfect, Thank you
Sergey Alexandrovich Kryukov 16-Dec-14 10:07am    
You are very welcome.
Good luck, call again.
—SA

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