Need a formula for calculating image position...?

DJJD

New member
Question is partially math and partially programming, but a non programmer should be able to answer. I have an image control which at any point could be stretched and sized to any size on the screen. Within that control is an actual picture, scaled proportionally to fit within the image control. Suppose the control is 300 x 400 and the image within the control is 1500 x 1200. When the image is proportional, depending on the size of the image control, there may be a gap on either the bottom or right of the image control (where there is no image). When the mouse pointer is positioned over the image control, I have no problem identifying the mouse position on the image control. However, considering the image is proportionally scaled within the control, i can't figure out how to calculate the actual position on the image its self which the pointer is over.
Currently, I have to stretch the image with no proportional scaling (right and bottom of image is stretched to right and bottom of image control). With it that way, I have no problems calculating it. However, I don't want it stretched, I want it to be proportionally scaled. My current formula (with image completely stretched, not proportionally) is as follows:
XImgPosition = (XCtrlPosition * (Img.Width / Ctrl.Width))
YImgPosition = (YCtrlPosition * (Img.Height / Ctrl.Height))
(Img = actual Image, Ctrl = Control displaying image)
I need a formula presuming the image is proportionally scaled, where there could be a gap in either the bottom or right side of the image on the control.
Should be a simple math formula, I don't think it's too hard. I just can't think how to do it, I don't have a mathematical mind.
Adding to the scenario, presuming control is 300x400 and image is 1500x1200, using my current formula:

XImgPosition = (XCtrlPosition * (1500 / 300))
YImgPosition = (YCtrlPosition * (1200 / 400))
 
Back
Top