简体   繁体   English

C# 绘图:绘制中间有孔的多边形的最佳方法是什么

[英]C# Drawing: What is the best way to draw a polygon with a hole in the middle

I have a shape that is defined by an outer border and then an inner border.我有一个由外边框和内边框定义的形状。 IF there is no inner boarder, the shape is solid.如果没有内边框,则形状是实心的。 If there is an inner border I want the polygon/path to be defined only between the two borders;如果有内部边界,我希望仅在两个边界之间定义多边形/路径; I don't want to draw the outside and then draw the inside in the background color.我不想画外面,然后用背景颜色画里面。

For example, if I have a square defined by the following coordinates for the outside border:例如,如果我有一个由外部边界坐标定义的正方形:

{0,0}, {20, 0}, {20,20}, {0, 20}

Then that square which is 20x20 with its bottom left right corner on the origin.然后是 20x20 的正方形,其左下角位于原点。 That shape then has a triangle cut out of the center:然后该形状从中心切出一个三角形:

{10,10}, {15,10}, {15,15}

How can I create a path that contains this shape using either WPF or GDI+?如何使用 WPF 或 GDI+ 创建包含此形状的路径?

You can draw that shape with XAML: (The key is to use a CombinedGeometry with GeometryCombineMode="Exclude" )您可以使用 XAML 绘制该形状:(关键是使用带有GeometryCombineMode="Exclude"CombinedGeometry

<Path Fill="Black">
    <Path.Data>
        <CombinedGeometry GeometryCombineMode="Exclude">
            <CombinedGeometry.Geometry1>
                <RectangleGeometry Rect="0,0,20,20"/>
            </CombinedGeometry.Geometry1>
            <CombinedGeometry.Geometry2>
                <PathGeometry>
                    <PathFigure StartPoint="10,10">
                        <LineSegment Point="15,10"/>
                        <LineSegment Point="15,15"/>
                    </PathFigure>
                </PathGeometry>
            </CombinedGeometry.Geometry2>
        </CombinedGeometry>
    </Path.Data>
</Path>

In GDI+, you can use FillPath() or DrawPath() with FillModeAlternate .在 GDI+ 中,您可以将FillPath()DrawPath()FillModeAlternate一起使用。

There's an example pretty close to what you're asking for here .有一个非常接近你在这里要求的例子。

Just draw the holes on top of the main contour using the background color.只需使用背景颜色在主轮廓顶部绘制孔。 It will look as if they were real holes edit use this approach if the api you are using does not support holes.如果您使用的 api 不支持孔,则它们看起来好像是真正的孔编辑使用此方法。 Which is why I assume you are asking这就是为什么我假设你在问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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