【问题标题】:How can I draw an arc in C# WPF with parameters?如何在 C# WPF 中使用参数绘制圆弧?
【发布时间】:2023-04-10 08:12:01
【问题描述】:

我想知道如何在 C# 中绘制弧线,我正在尝试使用 DrawEllipse,但它不起作用,而且绘制错误。 但是,我在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 });

【问题讨论】:

    标签: c# wpf drawingcontext


    【解决方案1】:

    您必须绘制一个包含圆弧段的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);
    }
    

    【讨论】:

    • 好的,没问题,你能告诉我如何设置端点吗? PathFigure中只有StartPoint
    • 查看答案。终点是 ArcSegment 的 Point 属性。
    • 我只有半径的值,如何只设置没有宽度和高度的半径?
    • 你不能。它是一个有两个半径的椭圆弧。请看一下文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多