I have a C programming background and I am used to doing ugly things.
Is there a more elegant/readable way to do this scaling operation in C#:
ImageProcessing.Resize(original, 80, (int)((float)original.Height * (80f / original.Width)));
Where "original" is the .Net Image class and Image.Width and Image.Height are thus integers.
Best How To :
Regular division followed by the floor operation (cast to
int) is equivalent to integer division. Multiplication and division are left-associative. Therefore you can use this much shorter code:
ImageProcessing.Resize(original, 80, 80 * original.Height / original.Width);
To convince you, suppose
200, respectively. Then your code will do this:
(int)(200f * (80f / 300f)) = (int)(200f * 0.2666...) = (int)(53.3333...) = 53
And my code will do this (where
/ denotes integer division):
80 * 200 / 300 = 16000 / 300 = 53