.Net 实现图片缩略图上传通用方法
日常开发中,经常碰到图片上传的需求,尤其在商城系统开发的时候,商品列表商品图片展示如果使用高清原图,由于高清原图比较大,加载原图时间会大大增加,直接导致系统性能底下,用户体验不好,并发量高的时候直接就挂掉了,这时候后台上传图片的时候,就必须将原高清图进行压缩,生成高质量缩略图,然后在商品列表读取缩略图可以大大减少加载时间,起到一个性能优化的作用,当然在商品详情的时候还是得用高清原图!
以下代码,可以在实际开发中使用将图片高质量压缩,话不多说,代码贴下:
//////生成缩略图或质量压缩 /// ///源图路径(物理路径) /// 缩略图路径(物理路径) /// 缩略图宽度,如果宽度为0则不缩略 /// 缩略图高度,如果高度为0则不缩略 /// 生成缩略图的方式,默认为空,为空则不缩略高宽[HW指定高宽缩放(不变形);W指定宽,高按比例;H指定高,宽按比例;CUT指定高宽裁减(不变形)] /// 压缩质量(数字越小压缩率越高)1-100 /// 压缩后图片的最大大小,0为不限制大小 publicstaticvoidMakeThumbnail(stringsourcePath,stringtargetPath,intwidth=0,intheight=0,stringmode="",intflag=100,intsize=0) { ImagesourceImage=null; Imagebitmap=null; Graphicsg=null; EncoderParametersep=null; EncoderParametereParam=null; try { sourceImage=Image.FromFile(sourcePath); inttoWidth=0; if(width>0) { toWidth=width; } else { toWidth=sourceImage.Width; } inttoHeight=0; if(height>0) { toHeight=height; } else { toHeight=sourceImage.Height; } intx=0; inty=0; intow=sourceImage.Width; intoh=sourceImage.Height; if(width>0&&height>0&&!string.IsNullOrWhiteSpace(mode)) { switch(mode.ToUpper()) { case"HW"://指定高宽缩放(不变形) inttempheight=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=newBitmap(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,newRectangle(0,0,toWidth,toHeight), newRectangle(x,y,ow,oh), GraphicsUnit.Pixel); //以下代码为保存图片时,设置压缩质量 ep=newEncoderParameters(); long[]qy=newlong[1]; qy[0]=flag;//设置压缩的比例1-100 eParam=newEncoderParameter(System.Drawing.Imaging.Encoder.Quality,qy); ep.Param[0]=eParam; ImageCodecInfo[]arrayICI=ImageCodecInfo.GetImageEncoders();//获取图像编码器的信息 ImageCodecInfojpegICIinfo=null; for(inti=0;i 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.Exceptionex) { throwex; } 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实现图片缩略图上传通用方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。