简体   繁体   中英

How to draw a smooth curved line in WPF?

I have three known positions, and currently I am driving two lines like so:

Line line = new Line
{
    StrokeThickness = 3,
    Stroke = lineColor,
    X1 = MyX,
    Y1 = MyY,
    X2 = MyX,
    Y2 = MiddleY
};

Graph.Children.Add(line);

line = new Line
{
    StrokeThickness = 3,
    Stroke = lineColor,
    X1 = MyX,
    Y1 = MiddleY,
    X2 = TargetX,
    Y2 = TargetY
};

Graph.Children.Add(line);

Here's the result:

在此输入图像描述

So, as you can see, this is almost what I want except that I want it to be more smoothed out, just a little bit.

Now I'm looking for any way I could set three points, set some smooth/curvy level to the middle point and then draw a line with a solid color on it. Much like how I can do this in Photoshop:

在此输入图像描述

Or at least get a similar kind of smoothness.

I think you are looking for splines

http://msdn.microsoft.com/en-us/library/554h284b.aspx

Gabe is correct that is from Forms

Under WPF you could try a PolyBezierSegment but it require 4 points. Possible you could put in three points and 1 more to shape it.

<Canvas>
    <Path Stroke="Black" StrokeThickness="10">
        <Path.Data>
            <PathGeometry>
                <PathGeometry.Figures>
                    <PathFigureCollection>    
                        <PathFigure StartPoint="100,80">
                            <PathFigure.Segments>
                                <PathSegmentCollection>
                                    <PolyBezierSegment Points="90,200 140,200 160,200 180,200 430,190 430,280" />
                                </PathSegmentCollection>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathFigureCollection>
                </PathGeometry.Figures>
            </PathGeometry>
        </Path.Data>
    </Path>
</Canvas>

This results in the following curve

在此输入图像描述

您想要使用PathFigure ,特别是使用一组BezierSegments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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