C#图像调整数学问题

Mic*_*lin 0 c# c#-4.0

所以我有一些简单的代码可以重新调整我的配置文件图像,因为它们被消耗了,问题是,C#代码没有按照我的预期运行...

这里是索引视图的Controller Action Method里面的一些代码,我在这里做...

    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;
Run Code Online (Sandbox Code Playgroud)

除非达到"reductionPercentage = * " ,否则它的每个部分都能完美运行

如果图像尺寸较小或相同,则reducePercentage完全按照预期的方式进行,并将值1指定给reductionPercentage,但是,如果图像较大,则表示它根本不进行数学计算,它总是吐出0作为reductionPercentage的值...

任何想法,我可能做错了什么?

Ode*_*ded 5

(282 / width)并且(337 / height)整数除法 - 当分母大于分子时,您将得到0结果.

使其中一个部门参与者浮动以确保浮点划分.

if (width >= height)
{
    reductionPercentage = (282f / width);
}
if (width < height)
{
    reductionPercentage = (337f / height);
}
Run Code Online (Sandbox Code Playgroud)