C#newbie问题与变量类型

ili*_*ica 1 c# double integer type-conversion

int newWidth = 100;
int newHeight = 100;
double ratio = 0;

if (img1.Width > img1.Height)
{
    ratio = img1.Width / img1.Height;
    newHeight = (int)(newHeight / ratio);
}
else
{
    ratio = img1.Height / img1.Width;
    newWidth = (int)(newWidth / ratio);
}

Image bmp1 = img1.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
bmp1.Save(Server.MapPath("~/Uploads/Photos/Thumbnails/") + photo.PhotoID + ".jpg");
Run Code Online (Sandbox Code Playgroud)

我总是得到具有相同值的高度和宽度的图像(100)
我在做类型转换时出错了吗?

Ant*_*ram 12

ratio = img1.Width / img1.Height;
Run Code Online (Sandbox Code Playgroud)

宽度和高度是整数.在将这些值存储到double中之前,您将对这些值执行整数运算.在整数数学中,150/100是1. 199/100是1. 101/100是1.没有小数.计算完值,它将存储在您的double中.

在进行计算之前,至少要将一侧投入两倍.

ratio = img1.Width / (double)img1.Height;
Run Code Online (Sandbox Code Playgroud)