Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had to solve this same problem today. I wanted it to work for images of any width:height ratio. </p> <p>Here's my method to find the point 'unscaled_p' on the original full-sized image.</p> <pre><code> Point p = pictureBox1.PointToClient(Cursor.Position); Point unscaled_p = new Point(); // image and container dimensions int w_i = pictureBox1.Image.Width; int h_i = pictureBox1.Image.Height; int w_c = pictureBox1.Width; int h_c = pictureBox1.Height; </code></pre> <p>The first trick is to determine if the image is a horizontally or vertically larger relative to the container, so you'll know which image dimension fills the container completely.</p> <pre><code> float imageRatio = w_i / (float)h_i; // image W:H ratio float containerRatio = w_c / (float)h_c; // container W:H ratio if (imageRatio &gt;= containerRatio) { // horizontal image float scaleFactor = w_c / (float)w_i; float scaledHeight = h_i * scaleFactor; // calculate gap between top of container and top of image float filler = Math.Abs(h_c - scaledHeight) / 2; unscaled_p.X = (int)(p.X / scaleFactor); unscaled_p.Y = (int)((p.Y - filler) / scaleFactor); } else { // vertical image float scaleFactor = h_c / (float)h_i; float scaledWidth = w_i * scaleFactor; float filler = Math.Abs(w_c - scaledWidth) / 2; unscaled_p.X = (int)((p.X - filler) / scaleFactor); unscaled_p.Y = (int)(p.Y / scaleFactor); } return unscaled_p; </code></pre> <p>Note that because Zoom centers the image, the 'filler' length has to be factored in to determine the dimension that is not filled by the image. The result, 'unscaled_p', is the point on the unscaled image that 'p' correlates to.</p> <p>Hope that helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload