I need to scale down an image that has a height or width greater than a predefined pixel value.
I wrote some code that takes a look at the original image, checks to
The solution posted by Nathaniel actually fails if the image height is larger than the image width. The following example yields the correct result :
private Size ResizeFit(Size originalSize, Size maxSize)
{
var widthRatio = (double)maxSize.Width / (double)originalSize.Width;
var heightRatio = (double) maxSize.Height/(double) originalSize.Height;
var minAspectRatio = Math.Min(widthRatio, heightRatio);
if (minAspectRatio > 1)
return originalSize;
return new Size((int)(originalSize.Width*minAspectRatio), (int)(originalSize.Height*minAspectRatio));
}