Skip to content

Instantly share code, notes, and snippets.

@MRezaSafari
Last active April 26, 2020 13:45
Show Gist options
  • Save MRezaSafari/ba2667ace29112c50bc2fd71edda88df to your computer and use it in GitHub Desktop.
Save MRezaSafari/ba2667ace29112c50bc2fd71edda88df to your computer and use it in GitHub Desktop.
calculate the aspect ratio of image
public Size CalculateAspectRation(int desiredWidth, int desiredHeight, Image image){
var widthScale = (double)desiredWidth / image.Width;
var heightScale = (double)desiredHeight / image.Height;
var scale = widthScale < heightScale ? widthScale : heightScale;
return new Size
{
Width = (int)(scale * image.Width),
Height = (int)(scale * image.Height)
};
}
function CalculateAspectRatio(desiredWidth, desiredHeight, width, height) {
var widthScale = parseFloat(desiredWidth / width);
var heightScale = parseFloat(desiredHeight / height);
var scale = widthScale < heightScale ? widthScale : heightScale;
return {
Width: parseInt(scale * width),
Height: parseInt(scale * height)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment