Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created October 14, 2020 08:53
Show Gist options
  • Save unitycoder/6999e19a1af0f78883fb1ebf44ed3a1e to your computer and use it in GitHub Desktop.
Save unitycoder/6999e19a1af0f78883fb1ebf44ed3a1e to your computer and use it in GitHub Desktop.
Calculate resize with aspect ratio (scale)
//// get aspect ratio https://stackoverflow.com/a/32593158/5452781
int sourceWidth = 3000;
int sourceHeight = 2000;
int targetWidth = 1920;
int targetHeight = 1080;
float percent = (new List<float> { (float)targetWidth / (float)sourceWidth, (float)targetHeight / (float)sourceHeight }).Min();
Size resultSize = new Size((int)Math.Floor(sourceWidth * percent), (int)Math.Floor(sourceHeight * percent));
Console.WriteLine("size= " + sourceWidth + "x" + sourceHeight + " percent= " + percent + " result=" + (sourceWidth * percent) + "x" + (sourceHeight * percent));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment