I have images that I want to display on a web page. I have user defined width and height limitations. If the original width of the image is > the user defined width or the original height of the image is > the user defined height then I need to shrink the image. The problem is that I need to shrink the image and maintain the aspect ratio. What is the formula to "fit" an image into a designated space while keeping its aspect ratio.

For example if MaxWidth = 10 and MaxHeight = 5 and the image is 12 x 6, I would want the image to be 10 x 5.

For example if MaxWidth = 10 and MaxHeight = 5 and the image is 6 x 12, I would want the image to be 2.5 x 5.

Keep in mind there are cases where the one dimension may fit, but then the other dimension is still too large which would require the image to be made smaller.

PLEASE HELP!!