ASP.NET 不经保存,直接生成上传图片的等比例的高质量缩略图

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">


  <title>不经保存,直接生成上传图片的等比例的高质量缩略图</title>
</head>
<body>

  <asp:FileUpload ID="FileUpload1" runat="server" />
  <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="上传并生成缩略图" />

</body>
</html>

 

后台代码 

protected void Button1_Click(object sender, EventArgs e)
        {
            String fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
            System.Drawing.Image image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(FileUpload1.FileBytes));
            int newWidth = 122, newHeight = 122;
            if ((decimal)image.Width / image.Height > (decimal)newWidth / newHeight)
            {
                newHeight = Convert.ToInt32((decimal)image.Height * newWidth / image.Width);
            }
            else if ((decimal)image.Width / image.Height < (decimal)newWidth / newHeight)
            {
                newWidth = Convert.ToInt32((decimal)image.Width * newHeight / image.Height);
            }
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newWidth, newHeight);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, newWidth, newHeight);
            g.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
            bmp.Save(Server.MapPath("~/") + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt);
            bmp.Dispose();
            image.Dispose();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值