嗨,在以下两种情况下,哪一种是最好的代码。
您可以看到我PercentW
在案例 1 中创建了变量。但在案例 2 中,我只是在更新NewWidth
情况1:
public static Image ResizeByWidth(Image Img, float NewWidth)
{
float PercentW = ((float)Img.Width / (float)NewWidth);
Bitmap bmp = new Bitmap(PercentW, (int)(Img.Height / PercentW));
Graphics g = Graphics.FromImage(bmp);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(Img, 0, 0, bmp.Width, bmp.Height);
g.Dispose();
return bmp;
}
案例二:
public static Image ResizeByWidth(Image Img, float NewWidth)
{
NewWidth = ((float)Img.Width / (float)NewWidth);
Bitmap bmp = new Bitmap(NewWidth, (int)(Img.Height / NewWidth));
Graphics g = Graphics.FromImage(bmp);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(Img, 0, 0, bmp.Width, bmp.Height);
g.Dispose();
return bmp;
}
谢谢