C#创建缩略图操作类实例

这篇文章主要介绍了C#创建缩略图操作类,实例分析了C#创建缩略图的相关技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了C#创建缩略图操作类。分享给大家供大家参考。具体分析如下:

这个C#类可以生成各种形式的缩略图,可以自动保持图片比例缩略,可以根据百分比获得图片尺寸等

 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;> /// 生成缩略图,并保持纵横比 ///  ///  ///  ///  /// 生成缩略图后对象 public static Image GetThumbnailImageKeepRatio(Image image, int width, int height) { Size imageSize = GetImageSize(image, width, height); return GetThumbnailImage(image, imageSize.Width, imageSize.Height); } ///  /// 根据百分比获取图片的尺寸 ///  ///  ///  ///  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); } ///  /// 根据设定的大小返回图片的大小,考虑图片长宽的比例问题 ///  ///  ///  ///  ///  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> 0) imageSize.Width = Convert.ToInt32(imageSize.Height * widthRatio); if (imageSize.Width > desiredWidth) { imageSize.Width = desiredWidth; imageSize.Height = Convert.ToInt32(imageSize.Width * heightRatio); } return imageSize; } ///  /// 获取图像编码解码器的所有相关信息 ///  /// 包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串 /// 返回图像编码解码器的所有相关信息 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)); } ///  /// 高质量保存图片 ///  ///  ///  ///  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#程序设计有所帮助。

以上就是C#创建缩略图操作类实例的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程