C#怎么改变图像的大小,比例

使用缩略图的方法就可以实现图片的大小变换,具体的方法如下:
System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(SourceImagePath));
int num = ((ThumbnailImageWidth / 4) * 3);
int width = image.Width;
int height = image.Height;
//计算图片的比例
if ((((double) width) / ((double) height)) >= 1.3333333333333333f)
{
num = ((height * ThumbnailImageWidth) / width);
}
else
{
ThumbnailImageWidth = ((width * num) / height);
}
if ((ThumbnailImageWidth < 1) || (num < 1))
{
return;
}
//用指定的大小和格式初始化 Bitmap 类的新实例 网管网www.bitscn.com
Bitmap bitmap = new Bitmap(ThumbnailImageWidth, num, PixelFormat.Format32bppArgb);
//从指定的 Image 对象创建新 Graphics 对象
Graphics graphics = Graphics.FromImage(bitmap);
//清除整个绘图面并以透明背景色填充
graphics.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制 原图片 对象
graphics.DrawImage(image, new Rectangle(0, 0, ThumbnailImageWidth, num));
image.Dispose();
try
{
//将此 原图片 以指定格式并用指定的编解码参数保存到指定文件
string savepath = (ThumbnailImagePath==null?SourceImagePath:ThumbnailImagePath);
SaveImage(bitmap,HttpContext.Current.Server.MapPath(savepath),GetCodecInfo((string)htmimes[sExt]));
}
catch(System.Exception e)
{
throw e;
}
finally
{
bitmap.Dispose();
graphics.Dispose();
}
}
#endregion

}
}

你可能感兴趣的:(image,String,C#)