C#創立縮略圖操作類實例。本站提示廣大學習愛好者:(C#創立縮略圖操作類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#創立縮略圖操作類實例正文
本文實例講述了C#創立縮略圖操作類。分享給年夜家供年夜家參考。詳細剖析以下:
這個C#類可以生成各類情勢的縮略圖,可以主動堅持圖片比例縮略,可以依據百分比取得圖片尺寸等
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; namespace HtmlSnap { public static class ImageHelper { /// <summary> /// 獲得縮略圖 /// </summary> /// <param name="image"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Image GetThumbnailImage(Image image, int width, int height) { if (image == null || width < 1 || height < 1) return null; // 新建一個bmp圖片 // Image bitmap = new System.Drawing.Bitmap(width, height); // 新建一個畫板 // using (Graphics g = System.Drawing.Graphics.FromImage(bitmap)) { // 設置高質量插值法 // g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; // 設置高質量,低速度出現膩滑水平 // g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // 高質量、低速度復合 // g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; // 清空畫布並以通明配景色填充 // 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> /// <param name="image"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns>生成縮略圖後對象</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> /// 依據百分比獲得圖片的尺寸 /// </summary> /// <param name="picture"></param> /// <param name="percent"></param> /// <returns></returns> 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> /// 依據設定的年夜小前往圖片的年夜小,斟酌圖片長寬的比例成績 /// </summary> /// <param name="picture"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> 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> /// 獲得圖象編碼解碼器的一切相干信息 /// </summary> /// <param name="mimeType">包括編碼解碼器的多用處網際郵件擴大協定 (MIME) 類型的字符串</param> /// <returns>前往圖象編碼解碼器的一切相干信息</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> /// 高質量保留圖片 /// </summary> /// <param name="image"></param> /// <param name="savePath"></param> /// <param name="ici"></param> private static void SaveImage(Image image, string savePath, ImageCodecInfo ici) { // 設置 原圖片 對象的 EncoderParameters 對象 // EncoderParameters parms = new EncoderParameters(1); EncoderParameter parm = new EncoderParameter(Encoder.Quality, ((long)95)); parms.Param[0] = parm; image.Save(savePath, ici, parms); parms.Dispose(); } } }
願望本文所述對年夜家的C#法式設計有所贊助。