C#图片压缩的实现方法

时间:2022-07-01 21:26:12 

一般在web应用中,对客户端提交上来的图片肯定需要进行压缩的。尤其是比较大的图片,如果不经过压缩会导致页面变的很大,打开速度比较慢,当然了如果是需要高质量的图片也得需要生产缩略图。

下面贴出我自己琢磨的图片压缩算法,首先这个是未经优化的简单实现:


public static System.Drawing.Image GetImageThumb(System.Drawing.Image sourceImg, int width, int height)
        {
            System.Drawing.Image targetImg = new System.Drawing.Bitmap(width, height);
            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(targetImg))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                g.DrawImage(sourceImg, new System.Drawing.Rectangle(0, 0, width, height), new System.Drawing.Rectangle(0, 0, sourceImg.Width, sourceImg.Height), System.Drawing.GraphicsUnit.Pixel);
                g.Dispose();
            }
            return targetImg;
        }


这个方法比较简单,用到的是高质量压缩。经过这个方法压缩后,200K的图片只能压缩到160k左右。经过改写代码实现了如下的方法:


public Bitmap GetImageThumb(Bitmap mg, Size newSize)
        {
            double ratio = 0d;
            double myThumbWidth = 0d;
            double myThumbHeight = 0d;
            int x = 0;
            int y = 0;

            Bitmap bp;

            if ((mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height /
            Convert.ToDouble(newSize.Height)))
                ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width);
            else
                ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height);
            myThumbHeight = Math.Ceiling(mg.Height / ratio);
            myThumbWidth = Math.Ceiling(mg.Width / ratio);

            Size thumbSize = new Size((int)newSize.Width, (int)newSize.Height);
            bp = new Bitmap(newSize.Width, newSize.Height);
            x = (newSize.Width - thumbSize.Width) / 2;
            y = (newSize.Height - thumbSize.Height);
            System.Drawing.Graphics g = Graphics.FromImage(bp);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
            g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel);

            return bp;
        }

这样实现的压缩使压缩率大幅度上升。其实代码并没有变多少,最主要的是在保存的时候要是用jpg格式,如果不指定格式,默认使用的是png格式。

下面这个是园友写的根据设置图片质量数值来压缩图片的方法:


public static bool GetPicThumbnail(string sFile, string outPath, int flag)
        {
            System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
            ImageFormat tFormat = iSource.RawFormat;

            //以下代码为保存图片时,设置压缩质量 
            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)
                {
                    iSource.Save(outPath, jpegICIinfo, ep);//dFile是压缩后的新路径 
                }
                else
                {
                    iSource.Save(outPath, tFormat);
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                iSource.Dispose();
                iSource.Dispose();
            }
        }

转载来源:http://www.cnblogs.com/lifeil/archive/2013/02/25/2931683.html

标签:图片压缩,图片压缩方法
0
投稿

猜你喜欢

  • SpringBoot集成整合JWT与Shiro流程详解

    2022-09-06 06:33:23
  • C#正则表达式Regex类的用法

    2022-06-07 13:44:58
  • Java java.sql.Timestamp时间戳案例详解

    2023-11-10 13:50:47
  • C#使用日志组件log4net

    2022-11-05 03:48:15
  • springboot实现注册加密与登录解密功能(demo)

    2021-12-15 15:10:08
  • Spring钩子接口汇总分析使用示例

    2022-09-24 23:29:56
  • commons fileupload实现文件上传的实例代码

    2021-08-22 16:53:00
  • Java基础教程之组合(composition)

    2022-08-02 19:12:32
  • Android 动态加载二维码视图生成快照的示例

    2023-08-04 19:57:54
  • C#清除WebBrowser中Cookie缓存的方法

    2022-01-13 20:01:08
  • SpringMvc框架的简介与执行流程详解

    2022-10-15 18:49:00
  • SpringBoot中异常处理实战记录

    2022-07-21 20:39:01
  • java实现京东秒杀功能分享 京东秒杀软件

    2021-09-10 23:27:36
  • Android实现双层ViewPager嵌套

    2021-12-23 02:35:52
  • MaterialApp Flutter 应用全局配置与主题管理详解

    2023-05-03 18:50:48
  • Filter过滤器和Listener监听器详解

    2021-08-20 07:39:38
  • 基于Java编写一个PDF与Word文件转换工具

    2023-05-30 19:23:12
  • 详谈OnTouchListener与OnGestureListener的区别

    2023-11-13 14:21:24
  • Flutter应用框架搭建实现屏幕适配方案详解

    2023-09-19 08:16:27
  • Java中BigDecimal类的add()的使用详解

    2023-03-07 16:12:11
  • asp之家 软件编程 m.aspxhome.com