<p>I have an application that displays an image inside of a Windows Forms <code>PictureBox</code> control.  The <code>SizeMode</code> of the control is set to <code>Zoom</code> so that the image contained in the <code>PictureBox</code> will be displayed in an aspect-correct way regardless of the dimensions of the <code>PictureBox</code>.</p>

<p>This is great for the visual appearance of the application because you can size the window however you want and the image will always be displayed using its best fit.  Unfortunately, I also need to handle mouse click events on the picture box and need to be able to translate from screen-space coordinates to image-space coordinates.</p>

<p>It looks like it's easy to translate from screen space to control space, but I don't see any obvious way to translate from control space to image space (i.e. the pixel coordinate in the source image that has been scaled in the picture box).</p>

<p>Is there an easy way to do this, or should I just duplicate the scaling math that they're using internally to position the image and do the translation myself?</p>

[{'answer_id': 2808, 'author': 'tags2k', 'author_id': 192, 'author_profile': 'https://Stackoverflow.com/users/192', 'pm_score': 3, 'selected': True, 'text': "<p>Depending on the scaling, the relative image pixel could be anywhere in a number of pixels.  For example, if the image is scaled down significantly, pixel 2, 10 could represent 2, 10 all the way up to 20, 100), so you'll have to do the math yourself and take full responsibility for any inaccuracies!  :-)</p>\n"}, {'answer_id': 3078, 'author': 'fastcall', 'author_id': 328, 'author_profile': 'https://Stackoverflow.com/users/328', 'pm_score': 3, 'selected': False, 'text': "<p>I wound up just implementing the translation manually.  The code's not too bad, but it did leave me wishing that they provided support for it directly.  I could see such a method being useful in a lot of different circumstances.</p>\n\n<p>I guess that's why they added extension methods :)</p>\n\n<p>In pseudocode:</p>\n\n<pre><code>// Recompute the image scaling the zoom mode uses to fit the image on screen\nimageScale ::= min(pictureBox.width / image.width, pictureBox.height / image.height)\n\nscaledWidth  ::= image.width * imageScale\nscaledHeight ::= image.height * imageScale\n\n// Compute the offset of the image to center it in the picture box\nimageX ::= (pictureBox.width - scaledWidth) / 2\nimageY ::= (pictureBox.height - scaledHeight) / 2\n\n// Test the coordinate in the picture box against the image bounds\nif pos.x &lt; imageX or imageX + scaledWidth &lt; pos.x then return null\nif pos.y &lt; imageY or imageY + scaledHeight &lt; pos.y then return null\n\n// Compute the normalized (0..1) coordinates in image space\nu ::= (pos.x - imageX) / imageScale\nv ::= (pos.y - imageY) / imageScale\nreturn (u, v)\n</code></pre>\n\n<p>To get the pixel position in the image, you'd just multiply by the actual image pixel dimensions, but the normalized coordinates allow you to address the original responder's point about resolving ambiguity on a case-by-case basis.</p>\n"}]
['https://Stackoverflow.com/questions/2804', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/328/']