這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)利用.net怎么將圖片等比縮放成縮略圖,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括遼中網(wǎng)站建設(shè)、遼中網(wǎng)站制作、遼中網(wǎng)頁(yè)制作以及遼中網(wǎng)絡(luò)營(yíng)銷(xiāo)策劃等。多年來(lái),我們專(zhuān)注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,遼中網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶(hù)以成都為中心已經(jīng)輻射到遼中省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶(hù)的支持與信任!/// <summary> /// 為圖片生成縮略圖 /// </summary> /// <param name="phyPath">原圖片的路徑</param> /// <param name="width">縮略圖寬</param> /// <param name="height">縮略圖高</param> /// <returns></returns> public System.Drawing.Image GetHvtThumbnail(System.Drawing.Image image, int width, int height) { Bitmap m_hovertreeBmp = new Bitmap(width, height); //從Bitmap創(chuàng)建一個(gè)System.Drawing.Graphics Graphics m_HvtGr = Graphics.FromImage(m_hovertreeBmp); //設(shè)置 m_HvtGr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //下面這個(gè)也設(shè)成高質(zhì)量 m_HvtGr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //下面這個(gè)設(shè)成High m_HvtGr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //把原始圖像繪制成上面所設(shè)置寬高的縮小圖 Rectangle rectDestination = new Rectangle(0, 0, width, height); int m_width, m_height; if (image.Width * height > image.Height * width) { m_height = image.Height; m_width = (image.Height * width) / height; } else { m_width = image.Width; m_height = (image.Width * height) / width; } m_HvtGr.DrawImage(image, rectDestination, 0, 0, m_width, m_height, GraphicsUnit.Pixel); return m_hovertreeBmp; }
C#縮略圖生成類(lèi),采用高質(zhì)量插值法實(shí)現(xiàn)縮略圖生成,高質(zhì)量,低速度呈現(xiàn)平滑程度,可以保持縮略圖縱橫比,在獲取縮略圖的時(shí)候一開(kāi)始就根據(jù)百分比獲取圖片的尺寸、根據(jù)設(shè)定的大小返回圖片的大小,并高質(zhì)量保存縮略圖圖片,為原圖片設(shè)置EncoderParameters 對(duì)象。
以下為類(lèi)文件,建議保存文件名為:ImageHelper.cs
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; namespace HtmlSnap { public static class ImageHelper { /// 獲取縮略圖 public static Image GetThumbnailImage(Image image, int width, int height) { if (image == null || width < 1 || height < 1) return null; // 新建一個(gè)bmp圖片 Image bitmap = new System.Drawing.Bitmap(width, height); // 新建一個(gè)畫(huà)板 using (Graphics g = System.Drawing.Graphics.FromImage(bitmap)) { // 設(shè)置高質(zhì)量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; // 設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // 高質(zhì)量、低速度復(fù)合 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; // 清空畫(huà)布并以透明背景色填充 g.Clear(Color.Transparent); // 在指定位置并且按指定大小繪制原圖片的指定部分 g.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); return bitmap; } } /// <summary> /// 生成縮略圖,并保持縱橫比 /// </summary> /// <returns>生成縮略圖后對(duì)象</returns> public static Image GetThumbnailImageKeepRatio(Image image, int width, int height) { Size imageSize = GetImageSize(image, width, height); return GetThumbnailImage(image, imageSize.Width, imageSize.Height); } /// <summary> /// 根據(jù)百分比獲取圖片的尺寸 /// </summary> public static Size GetImageSize(Image picture, int percent) { if (picture == null || percent < 1) return Size.Empty; int width = picture.Width * percent / 100; int height = picture.Height * percent / 100; return GetImageSize(picture, width, height); } /// <summary> /// 根據(jù)設(shè)定的大小返回圖片的大小,考慮圖片長(zhǎng)寬的比例問(wèn)題 /// </summary> public static Size GetImageSize(Image picture, int width, int height) { if (picture == null || width < 1 || height < 1) return Size.Empty; Size imageSize; imageSize = new Size(width, height); double heightRatio = (double)picture.Height / picture.Width; double widthRatio = (double)picture.Width / picture.Height; int desiredHeight = imageSize.Height; int desiredWidth = imageSize.Width; imageSize.Height = desiredHeight; if (widthRatio > 0) imageSize.Width = Convert.ToInt32(imageSize.Height * widthRatio); if (imageSize.Width > desiredWidth) { imageSize.Width = desiredWidth; imageSize.Height = Convert.ToInt32(imageSize.Width * heightRatio); } return imageSize; } /// <summary> /// 獲取圖像編碼解碼器的所有相關(guān)信息 /// </summary> /// <param name="mimeType">包含編碼解碼器的多用途網(wǎng)際郵件擴(kuò)充協(xié)議 (MIME) 類(lèi)型的字符串</param> /// <returns>返回圖像編碼解碼器的所有相關(guān)信息</returns> public static ImageCodecInfo GetCodecInfo(string mimeType) { ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo ici in CodecInfo) { if (ici.MimeType == mimeType) return ici; } return null; } public static ImageCodecInfo GetImageCodecInfo(ImageFormat format) { ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo icf in encoders) { if (icf.FormatID == format.Guid) { return icf; } } return null; } public static void SaveImage(Image image, string savePath, ImageFormat format) { SaveImage(image, savePath, GetImageCodecInfo(format)); } /// <summary> /// 高質(zhì)量保存圖片 /// </summary> private static void SaveImage(Image image, string savePath, ImageCodecInfo ici) { // 設(shè)置 原圖片 對(duì)象的 EncoderParameters 對(duì)象 EncoderParameters parms = new EncoderParameters(1); EncoderParameter parm = new EncoderParameter(Encoder.Quality, ((long)95)); parms.Param[0] = parm; image.Save(savePath, ici, parms); parms.Dispose(); } } }
上述就是小編為大家分享的利用.net怎么將圖片等比縮放成縮略圖了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享題目:利用.net怎么將圖片等比縮放成縮略圖-創(chuàng)新互聯(lián)
路徑分享:http://www.rwnh.cn/article12/dcisdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、品牌網(wǎng)站制作、建站公司、云服務(wù)器、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容