问题描述
用Graphics绘制的图像有白边,什么原因?如何解决?代码如下。///<summary>///等比缩放///</summary>///<paramname="sourceStream">源图片流</param>///<paramname="savePath">保存路径</param>///<paramname="maxWidth">最大宽度</param>///<paramname="maxHeight">最大高度</param>///<paramname="quality">压缩质量0-100</param>publicstaticboolUniformScale(StreamsourceStream,stringsavePath,doublemaxWidth,doublemaxHeight,intquality=100){ImagesourceImage=Image.FromStream(sourceStream);if(sourceImage.Width<128||sourceImage.Height<128){returnfalse;}//符合要求,无需缩放,直接保存。if(maxWidth>=sourceImage.Width&&maxHeight>=sourceImage.Height){sourceImage.Save(savePath,ImageFormat.Jpeg);returntrue;}else{//计算宽高doublewidthScale=sourceImage.Width/maxWidth;doubleheightScale=sourceImage.Height/maxHeight;doublescale=Math.Max(widthScale,heightScale);intw=(int)(sourceImage.Width/scale);inth=(int)(sourceImage.Height/scale);ImagenewImage=newBitmap(w,h);Graphicsg=Graphics.FromImage(newImage);//设置质量g.InterpolationMode=InterpolationMode.HighQualityBicubic;g.SmoothingMode=SmoothingMode.HighQuality;//画图g.DrawImage(sourceImage,0,0,w,h);//释放g.Dispose();//关键质量控制ImageCodecInfo[]codecInfoList=ImageCodecInfo.GetImageEncoders();ImageCodecInfocodeInfo=codecInfoList.SingleOrDefault(p=>p.MimeType=="image/jpeg");EncoderParametersep=newEncoderParameters();ep.Param[0]=newEncoderParameter(System.Drawing.Imaging.Encoder.Quality,(long)quality);try{//保存if(codeInfo!=null){newImage.Save(savePath,codeInfo,ep);}else{newImage.Save(savePath,ImageFormat.Jpeg);}returntrue;}catch{returnfalse;}finally{sourceImage.Dispose();newImage.Dispose();}}}
解决方案
解决方案二:
附图,白边情况如下。
解决方案三:
解决方案四:
俺也遇到这个问题,楼主有解决了吗?
解决方案五:
哪里有白边?图片另存后打开,没发现哪里有白边啊