I've been messing around with this problem for about an hour now.
I have a viewport which supports panning and zooming, the panning is handled by storing an offset for the X and Y axis. The zoom is just a float going from 0.2 to 14.
My problem is that I need to be able to place stuff where the user clicks in the viewport but when I zoom and pan the mouse coordinates are not correct. I haven't been able to figure out how to properly calculate the mouse coordinates.
Here's an image showing what I have so far: http://i.imgur.com/WQSXKJ2.png
As you can see the mouse origin is always at the top-left of the viewport component. You can see the pan X and Y offset as well as the zoom value in the bottom-left corner of the image. I've also added an example of the mouse coordinates in relation to the top-left of the viewport.
Now since in that image it's currently zoomed in the objects I place will be offset.
Thank you for your time!
EDITED WITH SOLUTION THAT WORKS FOR MY CASE:
void Viewport_MouseClick(object sender, MouseEventArgs e){
Point mousePosition = new Point((int)((e.X - Pan.X) / Zoom),
(int)((e.Y - Pan.Y) / Zoom));
}
This calculates a correct "screen-space" mouse position whilst taking the pan and zoom into account. I got that solution by playing around with TaWs answer. Thank you for the help! :)
Using a TrackBar for the Zoom and two more for the offsets this seems to work:
I have added the code for the Paint event, so you can see if that's the way your are handling it, too.
Put you picture in to PictureBox and then calculate the relative position of the mouse. That is
this code will give you the position of the cursor in the image.