.Net 实现图片缩略图上传通用方法
网络编程 2021-07-04 22:41www.168986.cn编程入门
这篇文章主要介绍了.Net 实现图片缩略图上传通用方法,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
日常开发中,经常碰到图片上传的需求,尤其在商城系统开发的时候,商品列表商品图片展示如果使用高清原图,由于高清原图比较大,加载原图时间会大大增加,直接导致系统性能底下,用户体验不好,并发量高的时候直接就挂掉了,这时候后台上传图片的时候,就必须将原高清图进行压缩,生成高质量缩略图,然后在商品列表读取缩略图可以大大减少加载时间,起到一个性能优化的作用,在商品详情的时候还是得用高清原图!
以下代码,可以在实际开发中使用将图片高质量压缩,话不多说,代码贴下
/// <summary> /// 生成缩略图或质量压缩 /// </summary> /// <param name="sourcePath">源图路径(物理路径)</param> /// <param name="targetPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度,如果宽度为0则不缩略</param> /// <param name="height">缩略图高度,如果高度为0则不缩略</param> /// <param name="mode">生成缩略图的方式,默认为空,为空则不缩略高宽[HW 指定高宽缩放(不变形);W 指定宽,高按比例;H 指定高,宽按比例;CUT 指定高宽裁减(不变形)]</param> /// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param> /// <param name="size">压缩后图片的最大大小,0为不限制大小</param> public static void MakeThumbnail(string sourcePath, string targetPath, int width = 0, int height = 0, string mode = "", int flag = 100, int size = 0) { Image sourceImage = null; Image bitmap = null; Graphics g = null; EncoderParameters ep = null; EncoderParameter eParam = null; try { sourceImage = Image.FromFile(sourcePath); int toWidth = 0; if (width > 0) { toWidth = width; } else { toWidth = sourceImage.Width; } int toHeight = 0; if (height > 0) { toHeight = height; } else { toHeight = sourceImage.Height; } int x = 0; int y = 0; int ow = sourceImage.Width; int oh = sourceImage.Height; if (width > 0 && height > 0 && !string.IsNullOrWhiteSpace(mode)) { switch (mode.ToUpper()) { case "HW"://指定高宽缩放(不变形) int tempheight = sourceImage.Height width / sourceImage.Width; if (tempheight > height) { toWidth = sourceImage.Width height / sourceImage.Height; } else { toHeight = sourceImage.Height width / sourceImage.Width; } break; case "W"://指定宽,高按比例 toHeight = sourceImage.Height width / sourceImage.Width; break; case "H"://指定高,宽按比例 toWidth = sourceImage.Width height / sourceImage.Height; break; case "CUT"://指定高宽裁减(不变形) if ((double)sourceImage.Width / (double)sourceImage.Height > (double)toWidth / (double)toHeight) { oh = sourceImage.Height; ow = sourceImage.Height toWidth / toHeight; y = 0; x = (sourceImage.Width - ow) / 2; } else { ow = sourceImage.Width; oh = sourceImage.Width height / toWidth; x = 0; y = (sourceImage.Height - oh) / 2; } break; } } //新建一个bmp图片 bitmap = new Bitmap(toWidth, toHeight); //新建一个画板 g = Graphics.FromImage(bitmap); g.CompositingQuality = CompositingQuality.HighQuality; //设置高质量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(sourceImage, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); //以下代码为保存图片时,设置压缩质量 ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = flag;//设置压缩的比例1-100 eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//获取图像编码器的信息 ImageCodecInfo jpegICIinfo = null; for (int i = 0; i < arrayICI.Length; i++) { if (arrayICI[i].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[i]; break; } } if (jpegICIinfo != null) { bitmap.Save(targetPath, jpegICIinfo, ep); FileInfo fiTarget = new FileInfo(targetPath); if (size > 0 && fiTarget.Length > 1024 size) { flag = flag - 10; MakeThumbnail(sourcePath, targetPath, width, height, mode, flag, size); } } else { //以jpg格式保存缩略图 bitmap.Save(targetPath, ImageFormat.Jpeg); } } catch (System.Exception ex) { throw ex; } finally { if (sourceImage != null) { sourceImage.Dispose(); } if (bitmap != null) { bitmap.Dispose(); } if (g != null) { g.Dispose(); } if (ep != null) { ep.Dispose(); } if (eParam != null) { eParam.Dispose(); } } }
以上所述是长沙网络推广给大家介绍的.Net 实现图片缩略图上传通用方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指