首页 文章

XAML UWP中System.Drawing.Drawing2D.GraphicsPath.AddArc的等效项

提问于
浏览
-1

在Xaml UWP渲染中,System.Drawing.Drawing2D.GraphicsPath.AddArc是否有任何等价物?

据我所知,我在XAML UWP中尝试过以下用例 .

Path path = new Path();
        PathGeometry pathGeometry = new PathGeometry();
        PathFigure pathFigure = new PathFigure();
        ArcSegment arcSegment = new ArcSegment();
        arcSegment.IsLargeArc = true;
        arcSegment.Size = new Windows.Foundation.Size(100, 100);
        arcSegment.Point = new Windows.Foundation.Point(100, 100);
        arcSegment.RotationAngle = 180;
        arcSegment.SweepDirection = SweepDirection.Clockwise;
        pathFigure.StartPoint = new Windows.Foundation.Point(100, 100);
        pathFigure.Segments.Add(arcSegment);
        path.Data = pathGeometry;
        path.Fill = new SolidColorBrush(Colors.Red);
        path.StrokeThickness = 2;
        pathGeometry.Figures.Add(pathFigure);

但以上并非100%相当于System.Drawing.Drawing2D.GraphicsPath.AddArc(Rectangle, Single, Single)

1 回答

  • 1

    在Xaml UWP渲染中,System.Drawing.Drawing2D.GraphicsPath.AddArc是否有任何等效物?

    在经典的Windows窗体应用程序中,我们有System.Drawing.Drawing2D名称空间用于绘制图形 . 但是UWP应用程序不支持此命名空间 . UWP应用程序有自己的绘制图形的API . 您应该能够在Windows.UI.Xaml.Shapes命名空间下通过API绘制形状 . 有关如何在uwp应用程序上绘制形状的更多详细信息,请参阅this article .

    方法 System.Drawing.Drawing2D.GraphicsPath.AddArc 实际上用于绘制弧,您已经在uwp app中找到了,您可以使用添加ArcSegment代替 .

    但以上并不是100%相当于System.Drawing.Drawing2D.GraphicsPath.AddArc(Rectangle,Single,Single)

    UWP应用程序有自己的绘制API,它可能没有与Windows窗体中的方法名称和参数相同的方法 . 但是你应该能够在uwp中使用其他方法来实现相同的效果 . 例如,AddArc方法有三个参数, Rectangle 用于定义x直径和y直径以及形状位置, startAnglesweepAngle 用于定义开始和结束角度 . 在uwp app中, ArcSegment 可以通过 size 属性定义弧x半径和y半径,并指定定义开始和结束位置 .

    例如,在windows窗体中,我们可以使用以下代码绘制弧形;

    protected override void OnPaint(PaintEventArgs e)
     {
         base.OnPaint(e);
         // Create a GraphicsPath object.
         GraphicsPath myPath = new GraphicsPath();
         // Set up and call AddArc, and close the figure.
         Rectangle rect = new Rectangle(20, 20, 50, 100);
         myPath.StartFigure();
         myPath.AddArc(rect, 0, 180);
         myPath.CloseFigure();
         // Draw the path to screen.
         e.Graphics.DrawPath(new Pen(Color.Red, 3), myPath);
     }
    

    在uwp应用程序中,您可以通过以下代码在xaml中执行相同的操作:

    <Path  StrokeThickness="2"  Margin="40,40,0,0" Stroke="Red">
         <Path.Data>
             <PathGeometry>
                 <PathFigure IsClosed="True">
                     <PathFigure.Segments>
                         <PathSegmentCollection>   
                             <ArcSegment  Size="25,50" Point="50,0"  IsLargeArc="True"  />  
                         </PathSegmentCollection>
                     </PathFigure.Segments>
                 </PathFigure>                 
             </PathGeometry>
         </Path.Data>
     </Path>
    

    或代码背后:

    private void btnCreatepath_Click(object sender, RoutedEventArgs e)
     {
         Path path = new Path();
         PathGeometry pathGeometry = new PathGeometry();
         PathFigure pathFigure = new PathFigure();
         ArcSegment arcSegment = new ArcSegment();
         arcSegment.IsLargeArc = true;
         arcSegment.Size = new Windows.Foundation.Size(25, 50);
         arcSegment.Point = new Windows.Foundation.Point(50, 0);
         arcSegment.RotationAngle = 180;
         pathFigure.IsClosed = true;
         pathFigure.Segments.Add(arcSegment);
         path.Data = pathGeometry;
         path.Stroke = new SolidColorBrush(Colors.Red);
         path.StrokeThickness = 2;
         path.Margin = new Thickness(40, 40, 0, 0);
         pathGeometry.Figures.Add(pathFigure);
         gridroot.Children.Add(path);
     }
    

    结果如下:

    enter image description here

    总而言之,您应该能够找到API来绘制您在表单中完成的相同操作 .

相关问题