# [ACCEPTED]-Resize Image to fit in bounding box-image-manipulation

Find which is smaller: `MaxWidth / w`

or `MaxHeight / h`

Then multiply 8 `w`

and `h`

by that number

**Explanation:**

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

To 6 find the scaling factor, `s`

, for the width, then 5 `s`

must be such that:
`s * w = MaxWidth`

.
Therefore, the 4 scaling factor is `MaxWidth / w`

.

Similarly for height.

The 3 one that requires the most scaling (smaller 2 `s`

) is the factor by which you must scale 1 the whole image.

Based on Eric's suggestion I'd do something 3 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 2 on the casts, but I was just trying to preserve 1 precision in the calculations.

To perform an aspect fill instead of an 8 aspect fit, use the larger ratio instead. That 7 is, change Matt's code from Math.Min to 6 Math.Max.

(An aspect fill leaves none of 5 the bounding box empty but may put some 4 of the image outside the bounds, while an 3 aspect fit leaves none of the image outside 2 the bounds but may leave some of the bounding 1 box empty.)

Tried Mr. Warren's code, but it didn't produce 4 reliable results.

For example,

```
ExpandToBound(new Size(640,480), new Size(66, 999)).Dump();
// {Width=66, Height=49}
ExpandToBound(new Size(640,480), new Size(999,50)).Dump();
// {Width=66, Height=50}
```

You can see, height 3 = 49 and height = 50 in another.

Here's mine 2 (based version of Mr. Warren's code) without 1 the discrepancy and a slight refactor:

```
// Passing null for either maxWidth or maxHeight maintains aspect ratio while
// the other non-null parameter is guaranteed to be constrained to
// its maximum value.
//
// Example: maxHeight = 50, maxWidth = null
// Constrain the height to a maximum value of 50, respecting the aspect
// ratio, to any width.
//
// Example: maxHeight = 100, maxWidth = 90
// Constrain the height to a maximum of 100 and width to a maximum of 90
// whichever comes first.
//
private static Size ScaleSize( Size from, int? maxWidth, int? maxHeight )
{
if ( !maxWidth.HasValue && !maxHeight.HasValue ) throw new ArgumentException( "At least one scale factor (toWidth or toHeight) must not be null." );
if ( from.Height == 0 || from.Width == 0 ) throw new ArgumentException( "Cannot scale size from zero." );
double? widthScale = null;
double? heightScale = null;
if ( maxWidth.HasValue )
{
widthScale = maxWidth.Value / (double)from.Width;
}
if ( maxHeight.HasValue )
{
heightScale = maxHeight.Value / (double)from.Height;
}
double scale = Math.Min( (double)(widthScale ?? heightScale),
(double)(heightScale ?? widthScale) );
return new Size( (int)Math.Floor( from.Width * scale ), (int)Math.Ceiling( from.Height * scale ) );
}
```

Following code produces more accurate results:

```
public static Size CalculateResizeToFit(Size imageSize, Size boxSize)
{
// TODO: Check for arguments (for null and <=0)
var widthScale = boxSize.Width / (double)imageSize.Width;
var heightScale = boxSize.Height / (double)imageSize.Height;
var scale = Math.Min(widthScale, heightScale);
return new Size(
(int)Math.Round((imageSize.Width * scale)),
(int)Math.Round((imageSize.Height * scale))
);
}
```

0

Python code, but maybe it will point you 1 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)
```

Über simple. :) The issue is to find a factor 5 by which you need to multiply width and 4 height. The solution is to try using one 3 and if it doesn't fit, use the other. So...

```
private float ScaleFactor(Rectangle outer, Rectangle inner)
{
float factor = (float)outer.Height / (float)inner.Height;
if ((float)inner.Width * factor > outer.Width) // Switch!
factor = (float)outer.Width / (float)inner.Width;
return factor;
}
```

To 2 fit picture (pctRect) to window (wndRect) call 1 like this

```
float factor=ScaleFactor(wndRect, pctRect); // Outer, inner
RectangleF resultRect=new RectangleF(0,0,pctRect.Width*factor,pctRect.Height*Factor)
```

Based on the previous answers, here's a 1 Javascript function:

```
/**
* fitInBox
* Constrains a box (width x height) to fit in a containing box (maxWidth x maxHeight), preserving the aspect ratio
* @param width width of the box to be resized
* @param height height of the box to be resized
* @param maxWidth width of the containing box
* @param maxHeight height of the containing box
* @param expandable (Bool) if output size is bigger than input size, output is left unchanged (false) or expanded (true)
* @return {width, height} of the resized box
*/
function fitInBox(width, height, maxWidth, maxHeight, expandable) {
"use strict";
var aspect = width / height,
initWidth = width,
initHeight = height;
if (width > maxWidth || height < maxHeight) {
width = maxWidth;
height = Math.floor(width / aspect);
}
if (height > maxHeight || width < maxWidth) {
height = maxHeight;
width = Math.floor(height * aspect);
}
if (!!expandable === false && (width >= initWidth || height >= initHeight)) {
width = initWidth;
height = initHeight;
}
return {
width: width,
height: height
};
}
```

Python code for this task based on Jason's 3 answer with fix for upscaling and argument 2 reorder for conventional argument passing 1 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
```

More Related questions

We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.