简体   繁体   English

使用C#在DrawingContext中绘制折线

[英]Draw Polyline in DrawingContext using c#

I want to draw a Polyline in the Visual Layer . 我想在Visual Layer绘制一条Polyline Here is the code I'm using to draw a Line . 这是我用来绘制Line的代码。 Should I draw multiple Lines and add them to the VisualCollection or there is a better way? 我应该画多条Lines并将其添加到VisualCollection还是有更好的方法?

var drawingVisual = new DrawingVisual();
using (var dc = drawingVisual.RenderOpen())
{
    var myPen = new Pen
    {
        Thickness = thickness,
        Brush = Settings.GridColor
    };
    myPen.Freeze();
    dc.DrawLine(myPen, pt1, pt2);
}

I think you would be better off using DrawGeometry than DrawLine . 我认为使用DrawGeometry比使用DrawLine更好。

Example: 例:

var myPen = new Pen
{
    Thickness = thickness,
    Brush = Settings.GridColor
};
myPen.Freeze();


var geometry = new StreamGeometry();
using (StreamGeometryContext ctx = geometry.Open())
{
    ctx.BeginFigure(new Point(10, 100), true /* is filled */, true /* is closed */);
    ctx.LineTo(new Point(100, 100), true /* is stroked */, false /* is smooth join */);
    ctx.LineTo(new Point(100, 50), true /* is stroked */, false /* is smooth join */);
}
geometry.Freeze();

dc.DrawGeometry(null, myPen, geometry);

I don't think there is a better way, except instead of calling myPen.Freeze() I would just create it as a const . 我认为没有更好的方法了,除了不调用myPen.Freeze()而是将其创建为const I don't know if that is really more efficient, I just think it clears the code up more in the future. 我不知道这是否真的更有效,我只是认为将来可以更清楚地清除代码。 I would also (personal thing) not use the var keyword, as it would be less ambiguous what type you create when you call drawingVisual.RenderOpen() . 我也不会(个人使用)使用var关键字,因为在调用drawingVisual.RenderOpen()时创建的类型将不太模糊。

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

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