StreamGeometry与DrawingContext.DrawLine在WPF C#中的比较

本文关键字:比较 WPF DrawingContext DrawLine StreamGeometry | 更新日期: 2023-09-27 17:58:01

我将实时绘制数百行。我选择了视觉层来执行此操作。但我看到这里有两种不同的划线方式。你建议哪一个能获得更好的性能和速度?

1.DrawingContext.DrawLine

public class DrawingTypeOne : FrameworkElement
{
    private readonly VisualCollection _visuals;
    public DrawingTypeOne(double thickness)
    {
        var myPen = new Pen
        {
            Thickness = 1,
            Brush = Brushes.White,
        };
        myPen.Freeze();
        _visuals = new VisualCollection(this);
        var drawingVisual = new DrawingVisual();
        using (var dc = drawingVisual.RenderOpen())
        {
            dc.DrawLine(myPen, new Point(0,0) , new Point(100,100));
            _visuals.Add(drawingVisual);
        }
    }
    protected override Visual GetVisualChild(int index)
    {
        return _visuals[index];
    }
    protected override int VisualChildrenCount
    {
        get
        {
            return _visuals.Count;
        }
    }
}

2.StreamGeometry

public class DrawingTypeTwo : FrameworkElement
{
    private readonly VisualCollection _visuals;
    public DrawingTypeTwo()
    {
        _visuals = new VisualCollection(this);
        var geometry = new StreamGeometry();
        using (var gc = geometry.Open())
        {
            gc.BeginFigure(new Point(0, 0), true, true);
            gc.LineTo(new Point(100,100), true, false);
        }
        geometry.Freeze();
        var drawingVisual = new DrawingVisual();
        using (var dc = drawingVisual.RenderOpen())
        {
            dc.DrawGeometry(Brushes.Red, null, geometry);
        }
        _visuals.Add(drawingVisual);
    }
    protected override Visual GetVisualChild(int index)
    {
        return _visuals[index];
    }
    protected override int VisualChildrenCount
    {
        get
        {
            return _visuals.Count;
        }
    }
}

StreamGeometry与DrawingContext.DrawLine在WPF C#中的比较

就像我说的,你只需要一个视觉,里面可以有你所有的线条。

看看这个:

首先,我们在绘图上下文中定义多个绘图:

class EllipseAndRectangle : DrawingVisual
{
    public EllipseAndRectangle()
    {
        using (DrawingContext dc = RenderOpen())
        {
            // Black ellipse with blue border
            dc.DrawEllipse(Brushes.Black,
                new Pen(Brushes.Blue, 3),        
                new Point(120, 120), 20, 40); 
            // Red rectangle with green border
            dc.DrawRectangle(Brushes.Red,
                new Pen(Brushes.Green, 4),    
                new Rect(new Point(10, 10), new Point(80, 80))); 
        }
    }
}

这是一个特殊的视觉或元素托管所有的图纸:

public class EllAndRectHost : FrameworkElement
{
    private EllipseAndRectangle _ellAndRect = new EllipseAndRectangle();
    // EllipseAndRectangle instance is our only visual child
    protected override Visual GetVisualChild(int index)
    {
        return _ellAndRect;
    }
    protected override int VisualChildrenCount
    {
        get
        {
            return 1;
        }
    }
}

这就是如何在XAML中使用所有这些东西:

<local:EllAndRectHost Margin="30" ... />

我说的是DrawingVisual类,你可以从中继承,而不是为100行创建100个视觉效果。

关于你的问题,第一种方法更快。因为第二种方法最终也是如此,所以第一种方法只是包装得很好。CCD_ 1是最低端。你不能比DrawLine更深入。CCD_ 3正在调用CCD_。