c# 如何實(shí)現(xiàn)圖片壓縮
一般在web應(yīng)用中,對客戶端提交上來的圖片肯定需要進(jìn)行壓縮的。尤其是比較大的圖片,如果不經(jīng)過壓縮會導(dǎo)致頁面變的很大,打開速度比較慢,當(dāng)然了如果是需要高質(zhì)量的圖片也得需要生產(chǎn)縮略圖。
下面貼出我自己琢磨的圖片壓縮算法,首先這個(gè)是未經(jīng)優(yōu)化的簡單實(shí)現(xiàn):
代碼如下:
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)了如下的方法:
代碼如下:
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ù)值來壓縮圖片寫的的方法:
代碼如下:
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(); ??????????? } ??????? }
以上就是c# 如何實(shí)現(xiàn)圖片壓縮的詳細(xì)內(nèi)容,更多關(guān)于c# 圖片壓縮的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Unity Shader相交算法實(shí)現(xiàn)簡易防能量盾
這篇文章主要為大家詳細(xì)介紹了Unity Shader相交算法實(shí)現(xiàn)簡易防能量盾,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C#正則表達(dá)式獲取下拉菜單(select)的相關(guān)屬性值
這篇文章主要介紹了C#正則表達(dá)式獲取下拉菜單(select)的相關(guān)屬性值,比如可以獲得name屬性的值、value值、指定值,需要的朋友可以參考下2014-07-07C#實(shí)現(xiàn)壓縮圖片為可控制的JPEG格式
這篇文章主要為大家詳細(xì)介紹了使用C#實(shí)現(xiàn)將圖片壓縮為質(zhì)量可自己控制的JPEG的幾種方式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-01-01c#之用戶定義的數(shù)據(jù)類型轉(zhuǎn)換介紹
c#允許定義自己的數(shù)據(jù)類型,這意味著需要某些工具支持在自己的數(shù)據(jù)類型間進(jìn)行數(shù)據(jù)轉(zhuǎn)換。方法是把數(shù)據(jù)類型轉(zhuǎn)換定義為相關(guān)類的一個(gè)成員運(yùn)算符,數(shù)據(jù)類型轉(zhuǎn)換必須聲明是隱式或者顯式,以說明怎么使用它2014-01-01C#數(shù)組學(xué)習(xí)相關(guān)資料整理
最近開始學(xué)習(xí)c#,并有幸接觸到了數(shù)組方便的操作,感覺確實(shí)不錯(cuò),這里簡單的整理下c#相關(guān)的學(xué)習(xí)資料,方便大家學(xué)習(xí)2012-09-09