ASP.NET 按规定尺寸缩放图片

 public string ImgFileUp(HttpPostedFile HPF, string saveDir)
    {    
        string returnStr1 = "";
        string Extension = new FileInfo(HPF.FileName).Extension;

        #region  保存原始文件
        //清除多余字符串
        saveDir = saveDir.TrimEnd('/').TrimStart('/');
        //获取上传日期
        string time = string.Concat("/", DateTime.Now.ToString("yyyyMMdd"));
        //判读上传文件夹是否存在
        bool existDir = Directory.Exists(Server.MapPath(string.Concat("~/", saveDir, time)));
        //不存在文件夹,自动创建
        if (!existDir)
        {
            //创建文件夹
            Directory.CreateDirectory(Server.MapPath(string.Concat("~/", saveDir, time)));
        }
        //上传的最初文件名+后缀
        string dbImageName = "";
        if (n == false)
        {

            dbImageName = System.IO.Path.GetFileName(HPF.FileName);
        }
        else
        {
            dbImageName = Guid.NewGuid() + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Extension;
        }
        
        //上传的图像保存的物理路径
        string originalImagePath = Server.MapPath(string.Concat("~/", saveDir, time, "/", dbImageName));
        //保存图片
        //HPF.SaveAs(originalImagePath); 
        //string originalImagePath = Server.MapPath("/" + saveDir + "/" + time + "/" + dbImageName);
        //生成压缩文件      
        System.Drawing.Image originalImage = System.Drawing.Image.FromStream(HPF.InputStream);
        int maxWidth =900;   //图片宽度最大限制
        int maxHeight = 900;  //图片高度最大限制
        int imgWidth = originalImage.Width;
        int imgHeight = originalImage.Height;
        if (imgHeight > maxHeight || imgWidth>maxWidth)  //如果宽度超或者高度超过进行压缩
        {
            if (imgWidth > imgHeight)
            {
                if (imgWidth > maxWidth)  //如果图片宽度超过限制
                {
                    float toImgWidth = maxWidth;   //图片压缩后的宽度 
                    float toImgHeight = imgHeight / (float)(imgWidth / toImgWidth); //图片压缩后的高度 
                    //新建一个bmp图片
                    System.Drawing.Image img = new System.Drawing.Bitmap(Convert.ToInt32(toImgWidth), Convert.ToInt32(toImgHeight));

                    Graphics g = System.Drawing.Graphics.FromImage(img);//新建画板 
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
                    g.Clear(Color.Transparent);//清空画布
                    g.DrawImage(originalImage, new Rectangle(0, 0, Convert.ToInt32(toImgWidth), Convert.ToInt32(toImgHeight)), new System.Drawing.Rectangle(0, 0, imgWidth, imgHeight), GraphicsUnit.Pixel);//绘制原图片
                    img.Save(originalImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                    originalImage.Dispose();
                    img.Dispose();
                    g.Dispose();
                }
            }
            else
            {
                if (imgHeight > maxHeight)
                {
                    float toImgHeight = maxHeight;  //图片压缩后的高度
                    float toImgWidth = imgWidth / (float)(imgHeight / toImgHeight); //图片压缩后的宽度
                    //新建一个bmp图片
                    System.Drawing.Image img = new System.Drawing.Bitmap(Convert.ToInt32(toImgWidth), Convert.ToInt32(toImgHeight));

                    Graphics g = System.Drawing.Graphics.FromImage(img);//新建画板 
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
                    g.Clear(Color.Transparent);//清空画布
                    g.DrawImage(originalImage, new Rectangle(0, 0, Convert.ToInt32(toImgWidth), Convert.ToInt32(toImgHeight)), new System.Drawing.Rectangle(0, 0, imgWidth, imgHeight), GraphicsUnit.Pixel);//绘制原图片
                    img.Save(originalImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                    originalImage.Dispose();
                    img.Dispose();
                    g.Dispose();

                }
            }
        }
        else
        {
            HPF.SaveAs(originalImagePath); 
        }

        returnStr1 = time + "/" + dbImageName;
        #endregion

        return returnStr1;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值