C#无损高质量压缩图片代码
本文实例为大家分享了C#无损高质量压缩图片的具体代码,供大家参考,具体内容如下
///无损压缩图片 ///原图片 /// 压缩后保存位置 /// 高度 /// /// 压缩质量(数字越小压缩率越高)1-100 /// publicstaticboolGetPicThumbnail(stringsFile,stringdFile,intdHeight,intdWidth,intflag) { System.Drawing.ImageiSource=System.Drawing.Image.FromFile(sFile); ImageFormattFormat=iSource.RawFormat; intsW=0,sH=0; //按比例缩放 Sizetem_size=newSize(iSource.Width,iSource.Height); if(tem_size.Width>dHeight||tem_size.Width>dWidth) { if((tem_size.Width*dHeight)>(tem_size.Width*dWidth)) { sW=dWidth; sH=(dWidth*tem_size.Height)/tem_size.Width; } else { sH=dHeight; sW=(tem_size.Width*dHeight)/tem_size.Height; } } else { sW=tem_size.Width; sH=tem_size.Height; } Bitmapob=newBitmap(dWidth,dHeight); Graphicsg=Graphics.FromImage(ob); g.Clear(Color.WhiteSmoke); g.CompositingQuality=CompositingQuality.HighQuality; g.SmoothingMode=SmoothingMode.HighQuality; g.InterpolationMode=InterpolationMode.HighQualityBicubic; g.DrawImage(iSource,newRectangle((dWidth-sW)/2,(dHeight-sH)/2,sW,sH),0,0,iSource.Width,iSource.Height,GraphicsUnit.Pixel); g.Dispose(); //以下代码为保存图片时,设置压缩质量 EncoderParametersep=newEncoderParameters(); long[]qy=newlong[1]; qy[0]=flag;//设置压缩的比例1-100 EncoderParametereParam=newEncoderParameter(System.Drawing.Imaging.Encoder.Quality,qy); ep.Param[0]=eParam; try { ImageCodecInfo[]arrayICI=ImageCodecInfo.GetImageEncoders(); ImageCodecInfojpegICIinfo=null; for(intx=0;x 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。