So I have a simple bit of code that re-sizes my profile images as they are consumed, problem is, the C# code isn’t working the way I expected…
Here’s the bit of code inside of the Controller Action Method for the Index View, where I’m doing this…
string fullFileName = HttpContext.Server.MapPath(profile.ProfilePhotoPath);
System.Drawing.Image img = System.Drawing.Image.FromFile(fullFileName);
int width = img.Width;
int height = img.Height;
float reductionPercentage = 0F;
if (width >= height)
{
reductionPercentage = (282 / width);
}
if (width < height)
{
reductionPercentage = (337 / height);
}
int newWidth = (int)Math.Round(width * reductionPercentage);
int newHeight = (int)Math.Round(height * reductionPercentage);
ViewBag.newWidth = newWidth;
ViewBag.newHeight = newHeight;
Every part of this works perfectly, except when it hits the “reductionPercentage = *“
If the image is smaller or the same size, the reductionPercentage does exactly as it should and assign the value 1 to the reductionPercentage, however, if the image is larger, it’s like it doesn’t do the math at all, it always spits out 0 as the value for the reductionPercentage…
Any ideas, what could I possibly doing wrong?
(282 / width)and(337 / height)are integer division – when the denominator is larger than the numerator, you will get0as a result.Make one of the division participants a float to ensure floating point division.