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

时间:2017-02-21 12:00:04

标签: c# xaml uwp uwp-xaml

在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 个答案:

答案 0 :(得分: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应用中找到了它,您可以使用添加ArcSegment代替。

  

但上面并不是100%等同于System.Drawing.Drawing2D.GraphicsPath.AddArc(Rectangle,Single,Single)

UWP应用程序有自己的绘图API,它可能没有与Windows窗体中的方法名称和参数相同的方法。但是你应该能够在uwp中使用其他方法来实现相同的效果。例如,AddArc方法有三个参数,Rectangle用于定义x直径和y直径以及形状位置,startAnglesweepAngle用于定义开始和结束角度。在uwp应用程序中,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来绘制您在表单中所做的相同的事情。