C#LearningTutorial:ResizeanImageinC#withAspectRatioandCropCenterImage使它没有间隙尺寸,如450w和250h,同时保持纵横比,但为了避免调整大小的图像,如侧面有间隙的肖像图像,我想裁剪图像的中心以填充调整大小的尺寸。我发现了大量代码可以在保持纵横比的同时调整图像大小,但不是我想要的,我希望有人能提供帮助。您应该通过needToFill=true:以上是C#学习教程:在C#中使用纵横比和裁剪中心图像调整图像大小,以便没有间隙如果对大家有用,我将分享所有内容,并且需要了解有关C#的更多信息,我希望您会更多关注教程——publicstaticSystem.Drawing.ImageFixedSize(Imageimage,intWidth,intHeight,boolneedToFill){#regioncalculationsintsourceWidth=image.Width;intsourceHeight=image.Height;int源X=0;int源Y=0;双destX=0;双destY=0;双nScale=0;双nScaleW=0;双nScaleH=0;nScaleW=((double)Width/(double)sourceWidth);nScaleH=((double)Height/(double)sourceHeight);如果(!needToFill){nScale=Math.Min(nScaleH,nScaleW);}else{nScale=Math.Max(nScaleH,nScaleW);destY=(Height-sourceHeight*nScale)/2;destX=(宽度-sourceWidth*nScale)/2;}如果(nScale>1)nScale=1;intdestWidth=(int)Math.Round(sourceWidth*nScale);intdestHeight=(int)Math.Round(sourceHeight*nScale);#endregionSystem.Drawing.BitmapbmPhoto=null;尝试{bmPhoto=newSystem.Drawing.Bitmap(destWidth+(int)Math.Round(2*destX),destHeight+(int)Math.Round(2*destY));}catch(Exceptionex){thrownewApplicationException(string.Format("destWidth:{0},destX:{1},destHeight:{2},desxtY:{3},Width:{4},Height:{5}",destWidth,destX,destHeight,destY,宽度,高度),ex);}使用(System.Drawing.GraphicsgrPhoto=System.Drawing.Graphics.FromImage(bmPhoto)){grPhoto.InterpolationMode=InterpolationMode.HighQualityBicubic;grPhoto.CompositingQuality=CompositingQuality.HighQuality;grPhoto.SmoothingMode=SmoothingMode.HighQuality;矩形=newSystem.Drawing.Rectangle((int)Math.Round(destX),(int)Math.Round(destY),destWidth,destHeight);矩形来自=newSystem.Drawing.Rectangle(sourceX,sourceY,sourceWidth,sourceHeight);//Console.WriteLine("发件人:"+from.ToString());//Console.WriteLine("收件人:"+to.ToString());grPhoto.DrawImage(图像,到,从,System.Drawing.GraphicsUnit.Pixel);返回bmPhoto;}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
