Home > Software design >  How can I draw an arc in C# WPF with parameters?
How can I draw an arc in C# WPF with parameters?

Time:10-25

I would like to know how can I draw an arc in C# , I'm trying using DrawEllipse but it doesn't work and it give wrong drawing. However, I have searched for a method to draw an arc in the Class DrawingContext but I didn't find it.

            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 });

CodePudding user response:

You would have to draw a PathGeometry or a StreamGeometry that contains an arc segment, like the following circular arc of radius 100 from (100,100) to (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);
}
  • Related