Last active
April 20, 2016 23:32
-
-
Save Bartmax/a2b49ee73fce0e7eb59797ee39369472 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Dimention2D ComputeSize(Image image, int? maxWidth, int? maxHeight) | |
{ | |
if (image == null) throw new ArgumentNullException(nameof(image)); | |
if (maxWidth <= 0) throw new ArgumentOutOfRangeException(nameof(maxWidth)); | |
if (maxHeight <= 0) throw new ArgumentOutOfRangeException(nameof(maxHeight)); | |
if (image.Width <= 0) throw new ArgumentOutOfRangeException(nameof(image.Width)); | |
if (image.Height <= 0) throw new ArgumentOutOfRangeException(nameof(image.Height)); | |
var sourceDimension = new Dimention2D(image.Width, image.Height); | |
bool computeWidth = sourceDimension.Width > maxWidth; | |
bool computeHeight = sourceDimension.Height > maxHeight; | |
if (computeWidth && computeHeight) | |
{ | |
if (sourceDimension.Width / maxWidth > sourceDimension.Height / maxHeight) | |
computeHeight = false; | |
else | |
computeWidth = false; | |
} | |
if (computeWidth) | |
return sourceDimension.ResizeWidth(maxWidth.Value); | |
if (computeHeight) | |
return sourceDimension.ResizeHeight(maxHeight.Value); | |
return sourceDimension; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment