分享

C#图片压缩方法

 weixinxiang 2014-10-13

C#图片压缩方法

来源:ab蓝学网整理     时间:2014-02-21    点击:32    欢迎网友投稿我来投稿
简介:这个是未经优化的简单实现publicstaticSystem.Drawing.ImageGetImageThumb(System.Drawing.Imageso
 这个是未经优化的简单实现 


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

如果觉得《C#图片压缩方法》不错,请把本站告诉您身边的朋友!

转载注明本文地址:http://www./prone_17401_1.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约