图片压缩

        /// <summary>
        /// 通过图片存储的物理路径以指定的宽高压缩图片并保存到指定的路径
        /// </summary>
        /// <param name="imgPath">图片物理路径</param>
        /// <param name="savePath">图片保存路径</param>
        ///  <param name="intWidth">压缩的宽度</param>
        /// <param name="intHeight">压缩的高度</param>
        /// <returns></returns>
        public void CompressImageByPath(string imgPath, string savePath, int imgWidth, int imgHeight)
        {
            FileStream fs = System.IO.File.OpenRead(imgPath);


            byte[] bigImg = new byte[fs.Length];


            fs.Read(bigImg, 0, bigImg.Length);
            fs.Close();
            if (bigImg != null && bigImg.Count() != 0)
            {
                using (System.IO.MemoryStream stream = new System.IO.MemoryStream(bigImg))
                {
                    System.Drawing.Image big = System.Drawing.Image.FromStream(stream);
                    if (((float)big.Width / big.Height) < ((float)imgWidth / imgHeight))
                    {
                        imgHeight = big.Height * imgWidth / big.Width;
                    }
                    else if (((float)big.Width / big.Height) > ((float)imgWidth / imgHeight))
                    {
                        imgWidth = big.Width * imgHeight / big.Height;
                    }
                    else if ((float)(big.Width / big.Height) == (imgWidth / imgHeight) && ((float)big.Height * imgWidth / big.Width) > ((float)big.Width * imgHeight / big.Height))
                    {
                        imgHeight = big.Height * imgWidth / big.Width;
                    }
                    else
                    {
                        imgWidth = big.Width * imgHeight / big.Height;
                    }
                    System.Drawing.Size size = new Size(imgHeight, imgHeight);


                    System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);


                    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);


                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;


                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;


                    g.Clear(Color.Transparent);


                    g.DrawImage(big, new System.Drawing.Rectangle(0, 0, size.Width, size.Height), new System.Drawing.Rectangle(0, 0, big.Width, big.Height), System.Drawing.GraphicsUnit.Pixel);
                    bitmap.Save(savePath, ImageFormat.Jpeg);
                    bitmap.Dispose();
                    big.Dispose();
                    g.Dispose();
                }
            }


        }
        /// <summary>
        /// 以指定的宽高压缩图片并保存到指定的路径
        /// </summary>
        /// <param name="big">图片</param>
        /// <param name="savePath">图片保存路径</param>
        ///  <param name="intWidth">压缩的宽度</param>
        /// <param name="intHeight">压缩的高度</param>
        /// <returns></returns>
        public void CompressImage(Image big, string savePath, int imgWidth, int imgHeight)
        {
            if (((float)big.Width / big.Height) < ((float)imgWidth / imgHeight))
           {
                imgHeight = big.Height * imgWidth / big.Width;
            }
            else if (((float)big.Width / big.Height) > ((float)imgWidth / imgHeight))
            {
                imgWidth = big.Width * imgHeight / big.Height;
            }
            else if ((float)(big.Width / big.Height) == (imgWidth / imgHeight) && ((float)big.Height * imgWidth / big.Width) > ((float)big.Width * imgHeight / big.Height))
            {
                imgHeight = big.Height * imgWidth / big.Width;
            }
            else
            {
                imgWidth = big.Width * imgHeight / big.Height;
            }
            System.Drawing.Size size = new Size(imgWidth, imgHeight);


            System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);


            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);


            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;


            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;


            g.Clear(Color.Transparent);


            g.DrawImage(big, new System.Drawing.Rectangle(0, 0, size.Width, size.Height), new System.Drawing.Rectangle(0, 0, big.Width, big.Height), System.Drawing.GraphicsUnit.Pixel);
            bitmap.Save(savePath, ImageFormat.Jpeg);
            bitmap.Dispose();
            g.Dispose();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值