From c77a52d6559c19d2bc8cc536d9b0cc6a1e5e3b08 Mon Sep 17 00:00:00 2001 From: mortenbock Date: Tue, 8 Apr 2014 15:26:37 +0200 Subject: [PATCH] Fix issue #43 Former-commit-id: b595749cba5c62e8a95dc1bf16d367b1078559ae --- src/ImageProcessor/Processors/Resize.cs | 40 +++++++++++++------------ 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/ImageProcessor/Processors/Resize.cs b/src/ImageProcessor/Processors/Resize.cs index f13ad84d8..e460e59f9 100644 --- a/src/ImageProcessor/Processors/Resize.cs +++ b/src/ImageProcessor/Processors/Resize.cs @@ -364,24 +364,26 @@ namespace ImageProcessor.Processors } } - // Constrain the image to fit the maximum possible height or width. - if (resizeMode == ResizeMode.Max) - { - if (sourceWidth > width || sourceHeight > height) - { - double ratio = Math.Abs(height / width); - double sourceRatio = Math.Abs(sourceHeight / sourceWidth); - - if (sourceRatio < ratio) - { - height = 0; - } - else - { - width = 0; - } - } - } + // Constrain the image to fit the maximum possible height or width. + if (resizeMode == ResizeMode.Max) + { + //If either is 0, we don't need to figure out orientation + if (width > 0 && height > 0) + { + //integers must be cast to doubles to get needed precision + double ratio = (double)height / width; + double sourceRatio = (double)sourceHeight / sourceWidth; + + if (sourceRatio < ratio) + { + height = 0; + } + else + { + width = 0; + } + } + } // If height or width is not passed we assume that the standard ratio is to be kept. if (height == 0) @@ -669,4 +671,4 @@ namespace ImageProcessor.Processors return floats; } } -} \ No newline at end of file +}