您的位置:首页 > 其它

图片的等比缩放

2016-04-24 23:41 375 查看
HttpPostedFile file = context.Request.Files[0];
using (Stream stream = file.InputStream)
{
Image img = new Bitmap(stream);
//3. 等比缩放图片 按照缩放大的比例来  200 * 100
int thumbW = 200, thumbH = 100;
int w = 0, h = 0;
if (img.Height * 1.0 / thumbH >= img.Width * 1.0 / thumbW)
{
h = thumbH;
w = (int)(thumbH * 1.0 / img.Height * img.Width);
}
else
{
w = thumbW;
h = (int)(thumbW * 1.0 / img.Width * img.Height);
}

using (Bitmap newImg = new Bitmap(w, h))
{
using (Graphics graphics = Graphics.FromImage(newImg))
{
graphics.DrawImage(img, new Rectangle(0, 0, w, h), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);

//1. 直接裁剪图片
//graphics.DrawImage(img, 0, 0, img.Width, img.Height);

//2. 指定 缩放 大小  100*100
//graphics.DrawImage(img, new Rectangle(0, 0, 100, 100), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);

newImg.Save(context.Server.MapPath("/b.png"));
context.Response.Write("Width: " + img.Width + "  height: " + img.Height);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: