wpf 将两个 Path 数字合二为一?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19114258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:45:44  来源:igfitidea点击:

Combine two Path figures in one?

c#wpfxamlpathgeometry

提问by Andrei Neculai

I'm having these two shapes:

我有这两种形状:

Figure 1Figure 2

图1图2

Fist picture code:

拳头图片代码:

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

Second picture code:

第二张图片代码:

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

How do i combine them for something like this(ignore the text): enter image description here

我如何将它们组合成这样的(忽略文本): 在此处输入图片说明

I need this to be done in C# ( not xaml ). Thanks!

我需要在 C# (不是 xaml )中完成这项工作。谢谢!

回答by Sheridan

For users wanting to know the XAML method

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

Basically, you can use a CombinedGeometryobject to do this:

基本上,您可以使用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>

Please take a look at the How to: Create a Combined Geometrypage on MSDN for full details.

请查看MSDN上的“如何:创建组合几何”页面以获取完整详细信息。

Please also note that you will need to set the StartPointproperty of these PathFigureobjects to correctly line up your shapes.

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

UPDATE >>>

更新 >>>

For users wanting to know the C# method

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

Sorry, I didn't notice that you wanted to use code. In code you would have to name your PathGeometryobjects and put them into a Resourcessection:

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

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

Then you can still use a CombinedGeometryobject:

然后你仍然可以使用一个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; 

Please take a look at the CombinedGeometry Classpage at MSDN for more information with this method.

请查看MSDN上的CombinedGeometry Class页面以获取有关此方法的更多信息。