使用外部线在DrawingContext上的WPF中绘制线条

时间:2012-04-20 16:14:18

标签: c# wpf line drawingcontext

我正在尝试在this问题中看到WPF中绘制很多类似的行,但区别在于我需要在DrawingVisual对象的DrawingContext上绘制它。怎么办呢?

1 个答案:

答案 0 :(得分:0)

你必须在彼此顶部的相同点之间绘制两条线,下面用较粗的笔,上面用较薄的笔。

Pen background = new Pen(Brushes.Black, 5);
Pen foreground = new Pen(Brushes.White, 3);

drawingContext.DrawLine(background, new Point(100, 100), new Point(200, 200));
drawingContext.DrawLine(foreground, new Point(100, 100), new Point(200, 200));

当您还定义笔线上限时,这条线看起来更好:

Pen background = new Pen
{
    Brush = Brushes.Black,
    Thickness = 5,
    StartLineCap = PenLineCap.Round,
    EndLineCap = PenLineCap.Round
};

Pen foreground = new Pen
{
    Brush = Brushes.White,
    Thickness = 3,
    StartLineCap = PenLineCap.Round,
    EndLineCap = PenLineCap.Round
};

使用像这样的扩展方法

public static class DrawingContextExtensions
{
    public static void DrawLine(this DrawingContext drawingContext,
        Pen background, Pen foreground, Point start, Point end)
    {
        drawingContext.DrawLine(background, start, end);
        drawingContext.DrawLine(foreground, start, end);
    }
}

你可以在一个电话中完成绘图:

drawingContext.DrawLine(background, foreground, new Point(100, 100), new Point(200, 200));