开发者

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 colo开发者_如何学JAVAr.

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. 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+?


You can draw that shape with XAML: (The key is to use a CombinedGeometry with GeometryCombineMode="Exclude")

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

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. Which is why I assume you are asking

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜