WPF指向路径几何

时间:2015-05-26 09:45:21

标签: c# wpf

我的画布150高度500宽。在这个画布中,我想添加一些点来生成一条线。生成此行不是问题。但我想生成一条在连接点之间弯曲的线,以获得平滑的曲线。线下面区域也必须填充颜色,如下例所示。

enter image description here

要循环这些点我使用以下简单代码:

Polyline line = new Polyline();

PointCollection collection = new PointCollection();

foreach (Point p in points)
{
    collection.Add(p);
}
line.Points = collection;
line.Stroke = new SolidColorBrush(Colors.Black);
line.StrokeThickness = 3;
canv.Children.Add(line);

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您可以使用

  

PolyBezierSegment

例如

 <Canvas>
        <Path Stroke="#FF56C0E9" StrokeThickness="10" Fill="#FFC0E5FC" >
            <Path.Data>
                <PathGeometry>
                    <PathGeometry.Figures>
                        <PathFigureCollection>
                            <PathFigure StartPoint="100,80" IsFilled="True">
                                <PathFigure.Segments>
                                    <PathSegmentCollection>
                                        <PolyBezierSegment Points="30 300,550 30,10 330" />
                                    </PathSegmentCollection>
                                </PathFigure.Segments>
                            </PathFigure>
                        </PathFigureCollection>
                    </PathGeometry.Figures>
                </PathGeometry>
            </Path.Data>
        </Path>
    </Canvas>

以上代码生成类似这样的曲线

enter image description here

  

您必须将PathFigure的IsFilled属性设置为True

可以从后面的代码完成同样的更多细节Check here

答案 1 :(得分:1)

我意识到OP要求一个平滑的形状,但对于那些寻找non-smooth alternative

的人来说
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Viewbox>
        <Path Stroke="Cyan" StrokeThickness="1" Fill="CornflowerBlue">
            <Path.Data>
                <PathGeometry>
                    <PathGeometry.Figures>
                        <PathFigure StartPoint="100,100" IsClosed="True">
                            <LineSegment Point="100,30" IsStroked="False" />
                            <LineSegment Point="110,40" />
                            <LineSegment Point="120,70" />
                            <LineSegment Point="130,50" />
                            <LineSegment Point="140,10" />
                            <LineSegment Point="150,50" />
                            <LineSegment Point="160,10" />
                            <LineSegment Point="170,30" />
                            <LineSegment Point="180,40" />
                            <LineSegment Point="190,60" />
                            <LineSegment Point="200,30" />
                            <LineSegment Point="200,100" IsStroked="False"/>
                            <LineSegment Point="100,100" IsStroked="False"/>
                        </PathFigure>
                    </PathGeometry.Figures>
                </PathGeometry>
            </Path.Data>
        </Path>
    </Viewbox>
</Page>