如何在 C# WPF 中绘制带有参数的圆弧?

How can I draw an arc in C# WPF with parameters?

我想知道如何在 C# 中绘制圆弧,我正在尝试使用 DrawEllipse,但它不起作用,并且绘制错误。 但是,我在 Class DrawingContext 中搜索了一种绘制圆弧的方法,但没有找到。

            DrawingVisual d = new DrawingVisual();

            System.Windows.Media.Pen pen = new System.Windows.Media.Pen();
            DrawingContext drawingContext = d.RenderOpen();

            pen.Brush = System.Windows.Media.Brushes.Black;
            System.Windows.Point center = new System.Windows.Point();
            center.X = 0.4;
            center.Y = 0.5;

            drawingContext.DrawEllipse(System.Windows.Media.Brushes.White, pen, center, 4,4);
            drawingContext.Close();
            canvas.Children.Add(new VisualHost { Visual = d });

您必须绘制包含弧段的 PathGeometryStreamGeometry,例如以下半径为 100 从 (100,100) 到 (200,200) 的圆弧:

var visual = new DrawingVisual();
var pen = new Pen(Brushes.Black, 1);

using (var dc = visual.RenderOpen())
{
    var figure = new PathFigure
    {
        StartPoint = new Point(100, 100) // start point
    };

    figure.Segments.Add(new ArcSegment
    {
        Point = new Point(200, 200), // end point
        Size = new Size(100, 100),   // radii
        SweepDirection = SweepDirection.Clockwise
    });

    var geometry = new PathGeometry();
    geometry.Figures.Add(figure);

    dc.DrawGeometry(null, pen, geometry);
}