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

发布于 2024-11-15 03:51:56 字数 323 浏览 4 评论 0原文

我有一个由外边框和内边框定义的形状。如果没有内边框,则形状是实心的。如果有内部边界,我希望仅在两个边界之间定义多边形/路径;我不想绘制外部,然后用背景颜色绘制内部。

例如,如果我有一个由以下坐标定义的外部边框的正方形:

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

那么该正方形的大小为 20x20,其左下角位于原点。然后,该形状从中心切出一个三角形:

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

如何使用 WPF 或 GDI+ 创建包含该形状的路径?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱&纷飞 2024-11-22 03:51:56

您可以使用 XAML 绘制该形状:(关键是使用 CombinedGeometryGeometryCombineMode="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>

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>
灯角 2024-11-22 03:51:56

在 GDI+ 中,您可以将 FillPath()DrawPath()FillModeAlternate 结合使用。

有一个非常接近您要求的示例 这里

In GDI+, you can use FillPath() or DrawPath() with FillModeAlternate.

There's an example pretty close to what you're asking for here.

享受孤独 2024-11-22 03:51:56

只需使用背景颜色在主轮廓顶部绘制孔即可。看起来就像是真正的洞
编辑
如果您使用的 api 不支持漏洞,请使用此方法。这就是为什么我假设你在问

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文