Skip to content

Instantly share code, notes, and snippets.

@Bartmax
Last active April 20, 2016 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bartmax/a2b49ee73fce0e7eb59797ee39369472 to your computer and use it in GitHub Desktop.
Save Bartmax/a2b49ee73fce0e7eb59797ee39369472 to your computer and use it in GitHub Desktop.
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