C# WPF指向路径几何体

C# WPF指向路径几何体,c#,wpf,C#,Wpf,我有一张150高500宽的帆布。在这个画布中,我想添加一些点来生成一条线。生成此行不是问题。但是我想生成一条在连接点之间弯曲的线,得到一条平滑的曲线。此外,线条下方的区域必须使用如下示例所示的颜色填充 要循环这些点,我使用以下简单代码: Polyline line = new Polyline(); PointCollection collection = new PointCollection(); foreach (Point p in points) { collection

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

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

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);
如何解决这个问题?

您可以使用

聚贝塞尔管段

举个例子

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

上面的代码生成一条类似这样的曲线

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


从代码隐藏到查看更多细节,也可以执行相同的操作

我知道OP要求的是平滑的形状,但是,对于那些需要以下形状的人:


处理非平滑线段序列的另一种方法是使用
多段线线段
代替
多段线线段
<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>