Skip to content

Instantly share code, notes, and snippets.

@chakrit
Created August 20, 2010 11:07
Show Gist options
  • Save chakrit/540086 to your computer and use it in GitHub Desktop.
Save chakrit/540086 to your computer and use it in GitHub Desktop.
private Size shrinkSize(Size origSize)
{
const int maxWidth = 900;
const int maxHeight = 700;
const double flipRatio = (double)maxWidth / maxHeight;
if (origSize.Width < maxWidth &&
origSize.Height < maxHeight)
return origSize;
// determine wether its better to shrink by height or by width
var ratio = (double)origSize.Width / origSize.Height;
if (ratio < flipRatio)
return new Size(
(int)(origSize.Width * ((double)maxHeight / origSize.Height)),
maxHeight);
else
return new Size(
maxWidth,
(int)(origSize.Height * ((double)maxWidth / origSize.Width)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment