简体   繁体   English

WPF C#中的StreamGeometry与DrawingContext.DrawLine

[英]StreamGeometry vs DrawingContext.DrawLine in WPF C#

I'm going to draw hundreds of lines in real-time. 我将实时绘制数百行 I have chosen the Visual Layer to do this. 我选择了Visual Layer来做到这一点。 But I see that there are two different ways to draw a line here. 但我看到有两种不同的方法可以在这里划一条线。 Which one you suggest to get a better performance and speed? 你建议哪一个获得更好的性能和速度?

1. DrawingContext.DrawLine 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 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;
        }
    }
}

Like I said you only need one visual and inside you can have all your lines. 就像我说你只需要一个视觉和内部你可以拥有你所有的线。

Take a look at this: 看看这个:

First we define multiple drawings inside our drawing context: 首先,我们在绘图上下文中定义多个绘图:

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

This is that one speical visual or element hosting all the drawings: 这是一个托付所有图纸的特殊视觉或元素:

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

And this is how you can use all those things in XAML: 这就是你如何在XAML中使用所有这些东西:

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

I was talking about the DrawingVisual class you could inherit from instead of creating 100 visuals for 100 lines. 我在谈论你可以继承的DrawingVisual类,而不是为100行创建100个视觉效果。

Regarding your question, first approach is faster. 关于你的问题,第一种方法更快。 Because the second approach is in the end doing the same the first does just its wrapped nicely. 因为第二种方法最终做同样的事情,第一种做法恰好包裹得很好。 DrawLine is the lowest end. DrawLine是最低端。 You can't go any deeper than DrawLine . 你不能比DrawLine更深入。 DrawGeometry is calling DrawLine and some other internal stuff. DrawGeometry正在调用DrawLine和其他一些内部DrawGeometry

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM