当前位置: 首页 > 编程语言 > C#

如何在C#中旋转图像时防止裁剪?Share

时间:2023-04-11 02:13:51 C#

C#旋转图片时如何防止裁剪?我刚刚经历了一些事情,试图弄清楚如何让图像均匀旋转。这有效,但现在它正在切割,我不确定如何让它停止......我正在使用这个rotateImage方法:publicstaticImageRotateImage(Imageimg,floatrotationAngle){//createanemptyBitmapimageBitmapbmp=newBitmap(img.Width,img.Height);//将Bitmap转换为Graphics对象Graphicsgfx=Graphics.FromImage(bmp);//现在我们将旋转点设置为图像的中心gfx.TranslateTransform((float)bmp.Width/2,(float)bmp.Height/2);//现在旋转图像gfx.RotateTransform(rotationAngle);gfx.TranslateTransform(-(float)bmp.Width/2,-(float)bmp.Height/2);//将InterpolationMode设置为HighQualityBicubic以确保在转换为指定大小后获得高质量的图像gfx.InterpolationMode=InterpolationMode.HighQualityBicubic;//现在将我们的新图像绘制到图形对象上gfx.DrawImage(img,newSystem.Drawing.Point(0,0));//处理我们的图形对象gfx.Dispose();//返回图像returnbmp;我尝试放大空位图,但只放大了一侧,因为图像固定在位图的左上角。任何想法将不胜感激!我在其他网站上找到了一些帮助。以下是我最想了解的人做的事情://将输入图像围绕中心旋转theta度。publicstaticBitmapRotateImage(BitmapbmpSrc,floattheta){MatrixmRotate=newMatrix();mRotate.Translate(bmpSrc.Width/-2,bmpSrc.Height/-2,MatrixOrder.Append);mRotate.RotateAt(theta,newSystem.Drawing.Point(0,0),MatrixOrder.Append);using(GraphicsPathgp=newGraphicsPath()){//通过旋转矩阵变换图像点gp.AddPolygon(newSystem.Drawing.Point[]{newSystem.Drawing.Point(0,0),newSystem.Drawing.Point(bmpSrc.Width,0),newSystem.Drawing.Point(0,bmpSrc.Height)});gp.Transform(mRotate);System.Drawing.PointF[]pts=gp.PathPoints;//创建大小为包含旋转的源图像的目标位图Rectanglebbox=boundingBox(bmpSrc,mRotate);位图bmpDest=newBitmap(bbox.Width,bbox.Height);using(GraphicsgDest=Graphics.FromImage(bmpDest)){//将源绘制到目标矩阵中mDest=newMatrix();mDest.Translate(bmpDest.Width/2,bmpDest.Height/2,MatrixOrder.Append);gDest.Transform=mDest;gDest.DrawImage(bmpSrc,pts);返回bmpDest;}}}privatestaticRectangleboundingBox(Imageimg,Matrixmatrix){GraphicsUnitgu=newGraphicsUnit();矩形rImg=Rectangle.Round(img.GetBounds(refgu));//变换图像的四个点,得到调整后的边界框。System.Drawing.PointtopLeft=newSystem.Drawing.Point(rImg.Left,rImg.Top);System.Drawing.PointtopRight=newSystem.Drawing.Point(rImg.Right,rImg.Top);System.Drawing.PointbottomRight=newSystem.Drawing.Point(rImg.Right,rImg.Bottom);System.Drawing.PointbottomLeft=newSystem.Drawing.Point(rImg.Left,rImg.Bottom);System.Drawing.Point[]points=newSystem.Drawing.Point[]{topLeft,topRight,bottomRight,bottomLeft};GraphicsPathgp=newGraphicsPath(points,newbyte[]{(byte)PathPointType.Start,(byte)PathPointType.Line,(byte)PathPointType.Line,(byte)PathPointType.Line});gp.Transform(矩阵);返回Rectangle.Round(gp.GetBounds());遵循这些步骤:创建一个空的目标图像,其宽度和高度=旋转图像的边界框将源图像绘制到目标图像上Source(0,0)将映射到目标(ox,oy)现在执行该步骤旋转目标图像。完成上述步骤的详细信息:W,H=目标的宽度和高度w,h=源的宽度和高度C=|cos(THETA)|S=|SIN(THETA)|w*c+h*s=W.w*s+h*c=H.ox=(W–w)/2oy=(H–h)/2publicBitmaprotateImage(Bitmapb,floatangle){if(angle>0){intl=b.宽度;inth=b.Height;doublean=角度*Math.PI/180;双cos=Math.Abs??(Math.Cos(an));双罪=Math.Abs??(Math.Sin(an));intnl=(int)(l*cos+h*sin);intnh=(int)(l*sin+h*cos);位图returnBitmap=newBitmap(nl,nh);图形g=图形.FromImage(returnBitmap);g.TranslateTransform((float)(nl-l)/2,(float)(nh-h)/2);g.TranslateTransform((float)b.Width/2,(float)b.Height/2);g.RotateTransform(角度);g.TranslateTransform(-(float)b.Width/2,-(float)b.Height/2);g.DrawImage(b,newPoint(0,0));返回返回位图;}否则返回b;}旋转后的图像可能需要包含更大的位图而不进行裁剪。计算边界的一种相当简单的方法是将变换矩阵应用于原始边界矩形的角。由于新的位图较大,原图必须绘制居中,而不是(0,0)。这在您的代码的以下修订中得到了证明:以上是C#学习教程:如何在C#中旋转图像时防止裁剪?如果分享的内容对你有用,需要进一步了解C#学习教程,希望你多多关注——publicstaticImageRotateImage(Imageimg,floatrotationAngle){intminx=int.MaxValue,maxx=int.MinValue,miny=int.MaxValue,maxy=int.MinValue;using(Bitmapbmp=newBitmap(1,1))//虚拟位图,因此我们可以使用TransformPoints计算出正确的大小。{使用(Graphicsg=Graphics.FromImage(bmp)){g.TranslateTransform((float)img.Width/2,(float)img.Height/2);g.RotateTransform(rotationAngle);g.TranslateTransform(-(float)img.Width/2,-(float)img.Height/2);Point[]pts=newPoint[4];点[0]=新点(0,0);pts[1]=newPoint(img.Width,0);pts[2]=newPoint(img.Width,img.Height);pts[3]=newPoint(0,img.Height);g.TransformPoints(CoordinateSpace.Device,CoordinateSpace.World,pts);foreach(Pointptinpts){minx=Math.Min(minx,pt.X);maxx=Math.Max(maxx,pt.X);miny=Math.Min(miny,pt.Y);maxy=Math.Max(maxy,点Y);}}}位图bmp2=newBitmap(maxx-minx,maxy-miny);使用(Graphicsg=Graphics.FromImage(bmp2)){g.TranslateTransform((float)bmp2.Width/2,(float)bmp2.Height/2);g.RotateTransform(rotationAngle);g.TranslateTransform(-(float)bmp2.Width/2,-(float)bmp2.Height/2);g.InterpolationMode=InterpolationMode.HighQualityBicubic;g.DrawImage(img,bmp2.Width/2-img.Width/2,bmp2.Height/2-img.Height/2);}返回bmp2;}本文收集自网络,不代表立场。如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处: