最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#圖片壓縮的實(shí)現(xiàn)方法

 更新時(shí)間:2013年02月25日 11:07:52   作者:  
一般在web應(yīng)用中,對(duì)客戶端提交上來的圖片肯定需要進(jìn)行壓縮的。尤其是比較大的圖片,如果不經(jīng)過壓縮會(huì)導(dǎo)致頁面變的很大,打開速度比較慢,當(dāng)然了如果是需要高質(zhì)量的圖片也得需要生產(chǎn)縮略圖。

一般在web應(yīng)用中,對(duì)客戶端提交上來的圖片肯定需要進(jìn)行壓縮的。尤其是比較大的圖片,如果不經(jīng)過壓縮會(huì)導(dǎo)致頁面變的很大,打開速度比較慢,當(dāng)然了如果是需要高質(zhì)量的圖片也得需要生產(chǎn)縮略圖。

下面貼出我自己琢磨的圖片壓縮算法,首先這個(gè)是未經(jīng)優(yōu)化的簡單實(shí)現(xiàn):

復(fù)制代碼 代碼如下:

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

這個(gè)方法比較簡單,用到的是高質(zhì)量壓縮。經(jīng)過這個(gè)方法壓縮后,200K的圖片只能壓縮到160k左右。經(jīng)過改寫代碼實(shí)現(xiàn)了如下的方法:

復(fù)制代碼 代碼如下:

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


這樣實(shí)現(xiàn)的壓縮使壓縮率大幅度上升。其實(shí)代碼并沒有變多少,最主要的是在保存的時(shí)候要是用jpg格式,如果不指定格式,默認(rèn)使用的是png格式。

下面這個(gè)是園友寫的根據(jù)設(shè)置圖片質(zhì)量數(shù)值來壓縮圖片的方法:

復(fù)制代碼 代碼如下:

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

            //以下代碼為保存圖片時(shí),設(shè)置壓縮質(zhì)量 
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = flag;//設(shè)置壓縮的比例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();
            }
        }


轉(zhuǎn)載來源:http://www.cnblogs.com/lifeil/archive/2013/02/25/2931683.html

相關(guān)文章

最新評(píng)論

镇沅| 文山县| 夏津县| 汝州市| 武功县| 宽甸| 荃湾区| 翁牛特旗| 永定县| 开平市| 临沧市| 宁南县| 枝江市| 方正县| 奈曼旗| 乌什县| 上思县| 四平市| 大庆市| 墨竹工卡县| 定边县| 罗平县| 南乐县| 西乌珠穆沁旗| 扎赉特旗| 太白县| 芜湖县| 民和| 金阳县| 成安县| 马山县| 沁水县| 盐亭县| 东山县| 永清县| 章丘市| 花莲市| 隆回县| 清水河县| 周口市| 萨迦县|