日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

C#實(shí)現(xiàn)圖片壓縮方法_.Net教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:C#如何在PPT中插入anychart圖表
Anychart 非常靈活的flash圖表控件,不僅可應(yīng)用于web,桌面和移動(dòng)應(yīng)用程序,而且還可以用于PowerPoint。用過之后就能體會(huì)到它的強(qiáng)大,下面我總結(jié)了如何在PPT中插入anychart圖表: 借助于anychart的 Save As Image功能,可以輕松地將你的anychart圖表嵌入到Powerpoint中

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

 

分享:asp.net中“從客戶端中檢測到有潛在危險(xiǎn)的Request.Form值”的錯(cuò)誤
在提交表單時(shí)候,asp.net 提示:從客戶端(......)中檢測到有潛在危險(xiǎn)的 Request.Form 值 。asp.net中的請求驗(yàn)證特性提供了某一等級的保護(hù)措施防止XSS攻擊,asp.net的請求驗(yàn)證是默認(rèn)啟動(dòng)的。這個(gè)給出各個(gè)版本.net的解決方法。 asp.net 2.0 通常解決辦法 方案一: 將.asp

來源:模板無憂//所屬分類:.Net教程/更新時(shí)間:2013-04-17
相關(guān).Net教程