C#图片压缩的实现方法

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

一般在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();
            }
        }

时间: 2024-07-31 16:12:19

C#图片压缩的实现方法的相关文章

图片压缩的具体方法你知道多少

在我们日常的工作中,会接触到很多图片文件,而有些图片还需要经常的进行大小的改变,如果一个网站对于图片有大小的限制,那么我们就需要将我们手中的图片压缩变小,图片压缩的具体方法你知道多少呢?下面小编为大家介绍一个压缩图片的方法,希望可以帮到你!参考迅捷压缩软件1:电脑上安装图片压缩软件,压缩软件有三个压缩的功能.点击图片压缩就可以. 2:添加要进行压缩的图片,点击页面中的添加文件或者添加文件夹的按钮,选择目标文件. 3:点击选择一个合适的压缩选项,在添加文件下有输出格式和压缩选项的设置,根据实际情况

图片压缩最保真方法

可以支持按比例裁剪和缩放图片,限制生成的图片大小,最多3次缩放就可以达到想要的图片. 代码: /** * @param bitmap 原图 * @param wh 宽高比 例:5:4 就是0.8 * @param maxBytes 最大字节数 例:小程序限制128k maxBytes就是1028*128 */ public static byte[] creatBitmapByWidth(Bitmap bitmap, float wh, int maxBytes) { //裁剪 int w =

android图片压缩的3种方法实例

android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream();        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中        int options = 100

android 图片压缩方法分析与学习

bitmap compress 是官方给出的图片质量压缩,通过试验学习了这个压缩的特性如下: 它是图片质量压缩,不会改变图片的分辨率 bitmap.compress(Bitmap.CompressFormat.JPEG, option, bos); 三个参数说明,1.图片压缩后的格式 2.图片压缩比例 3.压缩后得到的数据 这个方法会使图片压缩但是,由于是质量压缩,bitmap不会变小,也就是内存依然大,压缩的数据确实变小使用的时候得注意了内存溢出问题 测试方法如下:  System.out.p

Android图片压缩方法总结

本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). 第一:质量压缩方法: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 private Bitmap compressImage(Bitmap image) {           ByteArrayOutputStream baos = new ByteArrayOutputStream();         image

android图片压缩方法

第一:我们先看下质量压缩方法 private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; while ( baos.toByteArra

Android应用开发中三种常见的图片压缩方法

Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). 一.质量压缩法 private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里

(转)Android图片压缩方法总结

本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). http://www.open-open.com/lib/view/open1413862305997.html 第一:质量压缩方法: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 private Bitmap compressImage(Bitmap image) {           ByteArrayOutpu

Android 图片压缩的方法大全

public static Bitmap revitionImageSize(String path) throws IOException { BufferedInputStream in = new BufferedInputStream(new FileInputStream( new File(path))); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds =