5

I am allowing the user to resize an image while trying to maintain the original aspect ratio of the image.

For each resize operation I have an "offset" variable which indicates the width and height change. This is based on the mouse movement, so it could be any combination of values depending on how much they moved the mouse while resizing.

What I am doing now is taking the larger of the two values (x and y change) and using that to calculate the other value at the same aspect ratio. Here is my code:

if (Math.Abs(offset.X) > Math.Abs(offset.Y))
{
    offset.Y = (int)(offset.X / AspectRatio);
}
else
{
    offset.X = (int)(offset.Y * AspectRatio);
}

Aspect ratio is the standard width/height value.

The problem with my code is that it's using integer values so it's rounding and causing the aspect ratio to warp.

I assume what I need to do is snap to integer values that are evenly divisible by the aspect ratio, or something to that end. But I don't know how to do it by modifying these x and y "offset" values.

1
  • it's 2021, aspect-ratio is finally here, no need for hacks
    – Daniel
    Feb 19, 2021 at 10:04

2 Answers 2

2

Make offset.X,.Y and AspectRatio floats/doubles, do whatever calculations you need, and only once new sizes are calculated round it to integer.
This way you'll have maximum precision possible.

P.S. Use Math.Round instead of simply casting to int, this will gain you some extra precision.

1
  • +1; This is the right way to do the final size calculations. As for "extra precision", this isn't quite correct. It will give you no more precision, but definitely less error to your rounding. Nov 7, 2011 at 22:00
1

I assume what I need to do is snap to integer values that are evenly divisible by the aspect ratio, or something to that end. But I don't know how to do it by modifying these x and y "offset" values.

Either you will have an imperfect rounding of the aspect ratio, cropping, or chunky resizes. Think of this:

With a 3:4 aspect ratio, what is the minimum size you can scale horizontally and still perfectly match the original ratio?

Four pixels.

Now change your aspect ratio to two large prime numbers and you'll see the problem.

Instead of this, you can either crop the image in one dimension, or do interpolation using image filtering, and live with the not-quite-perfect aspect ratio change.

Make sure your calculations are correct, tho. Calculate your aspect ratio as a floating point value, and truncate to an integer only at the final calculation. Otherwise you will have double rounding errors.

3
  • 1
    You are right, using integers is not feasible. I can just convert it to floating-point for the purposes of storing the width/height and drawing the image. Nov 7, 2011 at 15:41
  • Svarog makes a good point about Math.Round giving you slightly more accurate answers. Doesn't give any rationale, but they do make good points about what you should actually do in the end - so make sure you round too :) Nov 7, 2011 at 21:58
  • Re: "Svarog makes a good point about Math.Round giving you slightly more accurate answers. Doesn't give any rationale" - here's the rationale - @svarog's answer says "P.S. Use Math.Round instead of simply casting to int, this will gain you some extra precision." -- a cast throws out the non-integral parts while rounding moves to the closest whole integer. For example, a cast to int will turn both 1.234 and 1.987 into integer 1, whereas Math.Round will produce 1 and 2 for same cases. Jun 9, 2014 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.