private void DrawLine() { // create a DrawingContext object DrawingContext dc = drawingVisual.RenderOpen(); // specify the start and end points of the line Point startPoint = new Point(10, 10); Point endPoint = new Point(50, 50); // set the line colors and thickness Pen pen = new Pen(Brushes.Black, 2); // draw the line dc.DrawLine(pen, startPoint, endPoint); // close the DrawingContext dc.Close(); }
private void DrawDashedLine() { // create a DrawingContext object DrawingContext dc = drawingVisual.RenderOpen(); // specify the start and end points of the line Point startPoint = new Point(10, 10); Point endPoint = new Point(50, 50); // set the line colors and thickness Pen pen = new Pen(Brushes.Black, 2); pen.DashStyle = new DashStyle(new double[] { 4, 4 }, 0); // draw the line dc.DrawLine(pen, startPoint, endPoint); // close the DrawingContext dc.Close(); }In both examples, the DrawingContext is created by calling the RenderOpen method on a DrawingVisual object. Once the line is drawn, the DrawingContext is closed by calling the Close method.