将两个Path数字合二为一?

时间:2013-10-01 10:48:56

标签: c# wpf xaml path geometry

我有这两种形状:

Figure 1 Figure 2

拳头图片代码:

<Path Fill="Orange">
    <Path.Data>
        <PathGeometry>
            <PathFigure StartPoint="0,100">
                <BezierSegment Point1="50,110" Point2="50,110" Point3="100,100"></BezierSegment>
                <LineSegment Point="100,80"></LineSegment>
                <LineSegment Point="120,90"></LineSegment>
                <LineSegment Point="120,70"></LineSegment>
                <LineSegment Point="100,60"></LineSegment>
                <LineSegment Point="100,20"></LineSegment>
                <LineSegment Point="80,0"></LineSegment>
                <LineSegment Point="20,0"></LineSegment>
                <LineSegment Point="0,20"></LineSegment>
                <LineSegment Point="0,40"></LineSegment>
                <LineSegment Point="-20,20"></LineSegment>
                <LineSegment Point="-20,40"></LineSegment>
                <LineSegment Point="0,60"></LineSegment>
            </PathFigure>
        </PathGeometry>
    </Path.Data>
</Path>

第二张图片代码:

<Path Fill="Orange">
    <Path.Data>
        <PathGeometry>
            <PathFigure>
                <LineSegment Point="0,25"></LineSegment>
                <LineSegment Point="250,25"></LineSegment>
                <LineSegment Point="250,0"></LineSegment>
            </PathFigure>
        </PathGeometry>
    </Path.Data>
</Path>

如何将它们组合成这样的东西(忽略文本): enter image description here

我需要在C#(而非xaml)中完成此操作。谢谢!

1 个答案:

答案 0 :(得分:7)

  

对于想要了解XAML方法的用户

基本上,您可以使用CombinedGeometry对象执行此操作:

<Path Stroke="Black" StrokeThickness="1" Fill="#CCCCFF">
    <Path.Data>
        <!-- Combines two geometries using the exclude combine mode. -->
        <CombinedGeometry GeometryCombineMode="Union">
            <CombinedGeometry.Geometry1>
                <PathGeometry>
                    <PathFigure StartPoint="0,100">
                        <BezierSegment Point1="50,110" Point2="50,110" Point3="100,100"></BezierSegment>
                        <LineSegment Point="100,80"></LineSegment>
                        <LineSegment Point="120,90"></LineSegment>
                        <LineSegment Point="120,70"></LineSegment>
                        <LineSegment Point="100,60"></LineSegment>
                        <LineSegment Point="100,20"></LineSegment>
                        <LineSegment Point="80,0"></LineSegment>
                        <LineSegment Point="20,0"></LineSegment>
                        <LineSegment Point="0,20"></LineSegment>
                        <LineSegment Point="0,40"></LineSegment>
                        <LineSegment Point="-20,20"></LineSegment>
                        <LineSegment Point="-20,40"></LineSegment>
                        <LineSegment Point="0,60"></LineSegment>
                    </PathFigure>
                </PathGeometry>
            </CombinedGeometry.Geometry1>
            <CombinedGeometry.Geometry2>
                <PathGeometry>
                    <PathFigure StartPoint="0,100">
                        <LineSegment Point="0,25"></LineSegment>
                        <LineSegment Point="250,25"></LineSegment>
                        <LineSegment Point="250,0"></LineSegment>
                    </PathFigure>
                </PathGeometry>
            </CombinedGeometry.Geometry2>
        </CombinedGeometry>
    </Path.Data>
</Path>

有关详细信息,请查看MSDN上的How to: Create a Combined Geometry页面。

另请注意,您需要设置这些StartPoint对象的PathFigure属性才能正确排列形状。

更新&gt;&gt;&gt;

  

对于想要了解C#方法的用户

抱歉,我没有注意到你想使用代码。在代码中,您必须为PathGeometry个对象命名,并将它们放入Resources部分:

<Application.Resources>
    <PathGeometry x:Key="Shape1">
        ...
    </PathGeometry>
    <PathGeometry x:Key="Shape2">
        ...
    </PathGeometry>
</Application.Resources>

然后你仍然可以使用CombinedGeometry对象:

PathGeometry shape1 = (PathGeometry)Application.Current.FindResource("Shape1");
PathGeometry shape2 = (PathGeometry)Application.Current.FindResource("Shape2");
CombinedGeometry combinedGeometry = new CombinedGeometry(
    GeometryCombineMode.Union, shape1, shape2);
Path combinedPath = new Path();
combinedPath.Data = combinedGeometry; 

有关此方法的详细信息,请查看MSDN上的CombinedGeometry Class页面。