Resize Image to fit in bounding box

Viewed 34938

An easy problem, but for some reason I just can't figure this out today.

I need to resize an image to the maximum possible size that will fit in a bounding box while maintaining the aspect ratio.

Basicly I'm looking for the code to fill in this function:

void CalcNewDimensions(ref int w, ref int h, int MaxWidth, int MaxHeight);

Where w & h are the original height and width (in) and the new height and width (out) and MaxWidth and MaxHeight define the bounding box that the image must fit in.

9 Answers

Find which is smaller: MaxWidth / w or MaxHeight / h Then multiply w and h by that number

Explanation:

You need to find the scaling factor which makes the image fit.

To find the scaling factor, s, for the width, then s must be such that: s * w = MaxWidth. Therefore, the scaling factor is MaxWidth / w.

Similarly for height.

The one that requires the most scaling (smaller s) is the factor by which you must scale the whole image.

Based on Eric's suggestion I'd do something like this:

private static Size ExpandToBound(Size image, Size boundingBox)
{       
    double widthScale = 0, heightScale = 0;
    if (image.Width != 0)
        widthScale = (double)boundingBox.Width / (double)image.Width;
    if (image.Height != 0)
        heightScale = (double)boundingBox.Height / (double)image.Height;                

    double scale = Math.Min(widthScale, heightScale);

    Size result = new Size((int)(image.Width * scale), 
                        (int)(image.Height * scale));
    return result;
}

I might have gone a bit overboard on the casts, but I was just trying to preserve precision in the calculations.

Python code, but maybe it will point you in the right direction:

def fit_within_box(box_width, box_height, width, height):
    """
    Returns a tuple (new_width, new_height) which has the property
    that it fits within box_width and box_height and has (close to)
    the same aspect ratio as the original size
    """
    new_width, new_height = width, height
    aspect_ratio = float(width) / float(height)

    if new_width > box_width:
        new_width = box_width
        new_height = int(new_width / aspect_ratio)

    if new_height > box_height:
        new_height = box_height
        new_width = int(new_height * aspect_ratio)

    return (new_width, new_height)

Python code for this task based on Jason's answer with fix for upscaling and argument reorder for conventional argument passing with img.shape.

def fit_within_box(box_height, box_width, height, width):
    """
    Returns a tuple (new_width, new_height) which has the property
    that it fits within box_width and box_height and has (close to)
    the same aspect ratio as the original size
    """
    new_width, new_height = width, height
    aspect_ratio = float(width) / float(height)

    if new_width > box_width or new_height < box_height:
        new_width = box_width
        new_height = int(new_width / aspect_ratio)

    if new_height > box_height or new_width < box_width:
        new_height = box_height
        new_width = int(new_height * aspect_ratio)

    return new_height, new_width
Related