惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - 飞舞的蒲公英

开发式新手引导设计思想 flash系统奔溃的主要原因 WinRAR(WinZip)压缩与解压实现(C#版Window平台) c#真正判断文件类型 winform文件拖入 c#winform选择文件,文件夹,打开指定目录方法 as3.0 动态文本属性大全 卡​马​克​卷​轴​算​法​研​究​_​地​图​双​缓​冲 春卷活动心得 As3 常用日期工具 As3 计算两个日期之间的天数差 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题 C#图片压缩算法 C#图片处理之: 另存为压缩质量可自己控制的JPEG C# :实现水印与图片合成,并利用Graphics 压缩图像质量 , (委托实现listBox的动态添加提示) C#放缩、截取、合并图片并生成高质量新图的类 手机游戏模拟器汇总 用于开发 SQL SERVER 2008 无法启动T-SQL调试的解决方法 WinAPI 操作串口
C#图片无损压缩
飞舞的蒲公英 · 2015-03-20 · via 博客园 - 飞舞的蒲公英

/引用命名空间
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)

        {

            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;

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

            }

            else

            {

                ob.Save(dFile, tFormat);

            }

            return true;

        }

        catch

        {

            return false;

        }

        finally

        {

            iSource.Dispose();

            ob.Dispose();

        }

    }

    #endregion

posted on 2015-03-20 20:24  飞舞的蒲公英  阅读(292)  评论()    收藏  举报