图片的等比缩放
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); } } }