I had to solve the same problem today. I wanted it to work for images of any width: height ratio.
Here is my method to find the point "unscaled_p" in the original full-size image.
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;
The first trick is to determine if the image is horizontal or vertical relative to the container, so you will find out what value of the image fills the container completely.
float imageRatio = w_i / (float)h_i; // image W:H ratio float containerRatio = w_c / (float)h_c; // container W:H ratio if (imageRatio >= 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)(pX / scaleFactor); unscaled_p.Y = (int)((pY - 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)((pX - filler) / scaleFactor); unscaled_p.Y = (int)(pY / scaleFactor); } return unscaled_p;
Please note that due to the fact that Zoom centers the image, it is necessary to determine the length of the βfillerβ in order to determine the size not filled by the image. The result of "unscaled_p" is a point in the unscaled image with which "p" correlates with.
Hope this helps!
dan
source share