Search code examples
wpfdrawingbezierdrawingcontext

WPF: Bezier curve using DrawingContext


I want to draw a bezier curve using DrawingContext class as follows:

var pathFigure = new PathFigure();
pathFigure.StartPoint = new Point(Width - 15, line.Position + line.Height);

pathFigure.Segments.Add(new BezierSegment(new Point(Width - 7.5, line.Position + line.Height - 20),new Point(Width, line.Position + line.Height + 20), new Point(Width + 7.5, line.Position + line.Height), false));
pathFigure.IsClosed = false;

var path = new PathGeometry();
path.Figures.Add(pathFigure);

drawingContext.DrawGeometry(Brushes.Black, _blackPen, path);

My _blackPen is initialized as follows:

 _blackPen = new Pen(Brushes.Black, 1);

But I get this result:

enter image description here

But what I want is something like what is shown in this link.

So what am I missing?

EDIT:

When I set pathFigure.IsFilled = false; the path disappears:

enter image description here

The same goes for drawingContext.DrawGeometry(null, _blackPen, path);


Solution

  • Do not fill the geometry.

    Either use a null Brush

    drawingContext.DrawGeometry(null, _blackPen, path);
    

    or set

    pathFigure.IsFilled = false;
    

    You also need to set the IsStroked property of the BezierSegment to true:

    pathFigure.Segments.Add(
        new BezierSegment(
            new Point(Width - 7.5, line.Position + line.Height - 20),
            new Point(Width, line.Position + line.Height + 20),
            new Point(Width + 7.5, line.Position + line.Height),
            true)); // here