标签:
我就不多说了注释里都有
if (Request.Files["file1"] == null) { Response.Write("<script>alert(\"请选择上传文件!\")</script>"); } else { //获取图片格式 string fileExtension = Path.GetExtension(Request.Files["file1"].FileName); if (fileExtension == ".jpg") { //图片保存路径 string savePath = Server.MapPath(@"~/uploadpic/"); //取到图片流 Stream sam = Request.Files["file1"].InputStream; //取到图片流存入Image System.Drawing.Image im = System.Drawing.Image.FromStream(sam); //原图宽度 int oWidth = im.Width; //原图高度 int oHeight = im.Height; //设置缩略图初始宽度 int tWidth = 210; //设置缩略图初始高度 int tHeight = 180; //创建空的bmp图片 Bitmap bt = new Bitmap(210, 180); //按比例计算出缩略图的宽度和高度 if (oWidth >= oHeight) tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth))); else tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight))); //图片居中 int pWidth = (210 - tWidth) / 2; int pHeight = (180 - tHeight) / 2; //创建绘制图片实例 Graphics g = Graphics.FromImage(bt); //控制图片质量 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low; //控制抗锯齿 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置透明背景 g.Clear(Color.Transparent); //生成缩略图 g.DrawImage(im, new Rectangle(pWidth, pHeight, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel); //保存图片 bt.Save(string.Format("{0}{1}.png", savePath, DateTime.Now.ToString("yyyyMMddHHmmss_yyyy")), ImageFormat.Png); } else { Response.Write("<script>alert(\"图片格式不正确!\")</script>"); } }
转载请注明!
标签:
原文地址:http://www.cnblogs.com/xuduo-123/p/4540337.html