C#图片压缩的实现方法

一般在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,width,height),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,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

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...