• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

.net等比缩放生成缩略图的方法

asp 搞代码 4年前 (2022-01-03) 33次浏览 已收录 0个评论

本文给大家汇总了2个C#中等比缩放实现生成缩略图的方法,第一种稍微简单些,第二种是本人常用的方法,这里推荐给大家,有需要的小伙伴可以参考下。

生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形

 /// <summary> /// 为图片生成缩略图 /// </summary> /// 原图片的路径 /// 缩略图宽 /// 缩略图高 ///  public System.Drawing.Image GetHvtThumbnail(System.Drawing.Image image, int width, int height) { Bitmap m_hovertreeBmp = new Bitmap(width, height); //从Bitmap创建一个System.Drawing.Graphics Graphics m_HvtGr = Graphics.FromImage(m_hovertreeBmp); //设置 m_HvtGr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //下面这个也设成高质量 m_HvtGr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //下面这个设成High m_HvtGr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //把原始图像绘制成上面所设置宽高的缩小图 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, GraphicsU<a style="color:transparent">来源gao($daima.com搞@代@#码网</a>nit.Pixel); return m_hovertreeBmp; } 

C#缩略图生成类,采用高质量插值法实现缩略图生成,高质量,低速度呈现平滑程度,可以保持缩略图纵横比,在获取缩略图的时候一开始就根据百分比获取图片的尺寸、根据设定的大小返回图片的大小,并高质量保存缩略图图片,为原图片设置EncoderParameters 对象。

以下为类文件,建议保存文件名为: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  /// 生成缩略图,并保持纵横比 /// </summary> /// 生成缩略图后对象 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> 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> public static Size GetImageSize(Image picture, int width, int height) { if (picture == null || width <1 || 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; } /// <summary> /// 获取图像编码解码器的所有相关信息 /// </summary> /// 包含编码解码器的多用途网际邮件扩充协议 (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)); } /// <summary> /// 高质量保存图片 /// </summary> 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(); } } } 

以上就是.net等比缩放生成缩略图的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:.net等比缩放生成缩略图的方法
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址