• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#图片无损压缩

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

/引用命名空间
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;
#region GetPicThumbnail
    /// <summary>
    /// 无损压缩图片
    /// </summary>
    /// <param name="sFile">原图片</param>
    /// <param name="dFile">压缩后保存位置</param>
    /// <param name="dHeight">高度</param>
    /// <param name="dWidth"></param>
    /// <param name="flag">压缩质量 1-100</param>
    /// <returns></returns>
 
    public bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth, int flag)
    {
        System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
 
        ImageFormat tFormat = iSource.RawFormat;
 
        int sW = 0, sH = 0;
 
        //按比例缩放
 
        Size tem_size = new Size(iSource.Width, iSource.Height);
 
 
 
        if (tem_size.Width > dHeight || tem_size.Width > dWidth) //将**改成c#中的或者操作符号
        {
 
            if ((tem_size.Width * dHeight) > (tem_size.Height * dWidth))
            {
 
                sW = dWidth;
 
                sH = (dWidth * tem_size.Height) / tem_size.Width;
 
            }
 
            else
            {
 
                sH = dHeight;
 
                sW = (tem_size.Width * dHeight) / tem_size.Height;
 
            }
 
        }
 
        else
        {
 
            sW = tem_size.Width;
 
            sH = tem_size.Height;
 
        }
 
        Bitmap ob = new Bitmap(dWidth, dHeight);
 
        Graphics g = Graphics.FromImage(ob);
 
        g.Clear(Color.WhiteSmoke);
 
        g.CompositingQuality = CompositingQuality.HighQuality;
 
        g.SmoothingMode = SmoothingMode.HighQuality;
 
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
 
        g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
 
        g.Dispose();
 
        //以下代码为保存图片时,设置压缩质量
 
        EncoderParameters ep = new EncoderParameters();
 
        long[] qy = new long[1];
 
        qy[0] = flag;//设置压缩的比例1-100
 
        EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
 
        ep.Param[0] = eParam;
 
        try
        {
 
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
 
            ImageCodecInfo jpegICIinfo = null;
 
            for (int x = 0; x < arrayICI.Length; x++)
            {
 
                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {
 
                    jpegICIinfo = arrayICI[x];
 
                    break;
 
                }
 
            }
 
            if (jpegICIinfo != null)
            {
 
                ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
 
            }
 
            else
            {
 
                ob.Save(dFile, tFormat);
 
            }
 
            return true;
 
        }
 
        catch
        {
 
            return false;
 
        }
 
        finally
        {
 
            iSource.Dispose();
 
            ob.Dispose();
 
        }
 
 
 
    }
    #endregion

/引用命名空间
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;
#region GetPicThumbnail
    /// <summary>
    /// 无损压缩图片
    /// </summary>
    /// <param name="sFile">原图片</param>
    /// <param name="dFile">压缩后保存位置</param>
    /// <param name="dHeight">高度</param>
    /// <param name="dWidth"></param>
    /// <param name="flag">压缩质量 1-100</param>
    /// <returns></returns>
 
    public bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth, int flag)
    {
        System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
 
        ImageFormat tFormat = iSource.RawFormat;
 
        int sW = 0, sH = 0;
 
        //按比例缩放
 
        Size tem_size = new Size(iSource.Width, iSource.Height);
 
 
 
        if (tem_size.Width > dHeight || tem_size.Width > dWidth) //将**改成c#中的或者操作符号
        {
 
            if ((tem_size.Width * dHeight) > (tem_size.Height * dWidth))
            {
 
                sW = dWidth;
 
                sH = (dWidth * tem_size.Height) / tem_size.Width;
 
            }
 
            else
            {
 
                sH = dHeight;
 
                sW = (tem_size.Width * dHeight) / tem_size.Height;
 
            }
 
        }
 
        else
        {
 
            sW = tem_size.Width;
 
            sH = tem_size.Height;
 
        }
 
        Bitmap ob = new Bitmap(dWidth, dHeight);
 
        Graphics g = Graphics.FromImage(ob);
 
        g.Clear(Color.WhiteSmoke);
 
        g.CompositingQuality = CompositingQuality.HighQuality;
 
        g.SmoothingMode = SmoothingMode.HighQuality;
 
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
 
        g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
 
        g.Dispose();
 
        //以下代码为保存图片时,设置压缩质量
 
        EncoderParameters ep = new EncoderParameters();
 
        long[] qy = new long[1];
 
        qy[0] = flag;//设置压缩的比例1-100
 
        EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
 
        ep.Param[0] = eParam;
 
        try
        {
 
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
 
            ImageCodecInfo jpegICIinfo = null;
 
            for (int x = 0; x < arrayICI.Length; x++)
            {
 
                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {
 
                    jpegICIinfo = arrayICI[x];
 
                    break;
 
                }
 
            }
 
            if (jpegICIinfo != null)
            {
 
                ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
 
            }
 
            else
            {
 
                ob.Save(dFile, tFormat);
 
            }
 
            return true;
 
        }
 
        catch
        {
 
            return false;
 
        }
 
        finally
        {
 
            iSource.Dispose();
 
            ob.Dispose();
 
        }
 
 
 
    }
    #endregion

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#调用WindowsAPI之GetClientRect用法发布时间:2022-07-13
下一篇:
C#动态调用泛型类、泛型方法发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap