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;
    }

你可能感兴趣的:(ASP.NET)