您好,欢迎访问三七文档
publicstaticBitmapGetThumbnail(Bitmapb,intdestHeight,intdestWidth){System.Drawing.ImageimgSource=b;System.Drawing.Imaging.ImageFormatthisFormat=imgSource.RawFormat;intsW=0,sH=0;//按比例缩放intsWidth=imgSource.Width;intsHeight=imgSource.Height;if(sHeightdestHeight||sWidthdestWidth){if((sWidth*destHeight)(sHeight*destWidth)){sW=destWidth;sH=(destWidth*sHeight)/sWidth;}else{sH=destHeight;sW=(sWidth*destHeight)/sHeight;}}else{sW=sWidth;sH=sHeight;}BitmapoutBmp=newBitmap(destWidth,destHeight);Graphicsg=Graphics.FromImage(outBmp);g.Clear(Color.Transparent);//设置画布的描绘质量g.CompositingQuality=CompositingQuality.HighQuality;g.SmoothingMode=SmoothingMode.HighQuality;g.InterpolationMode=InterpolationMode.HighQualityBicubic;g.DrawImage(imgSource,newRectangle((destWidth-sW)/2,(destHeight-sH)/2,sW,sH),0,0,imgSource.Width,imgSource.Height,GraphicsUnit.Pixel);g.Dispose();//以下代码为保存图片时,设置压缩质量EncoderParametersencoderParams=newEncoderParameters();long[]quality=newlong[1];quality[0]=100;EncoderParameterencoderParam=newEncoderParameter(System.Drawing.Imaging.Encoder.Quality,quality);encoderParams.Param[0]=encoderParam;imgSource.Dispose();returnoutBmp;}
本文标题:c图片按比例缩放
链接地址:https://www.777doc.com/doc-2908453 .html