用 GDI 勾画出一条路径+在.Net中

发布于 2024-08-07 19:29:57 字数 194 浏览 1 评论 0原文

如何使用 GDI+ 绘制图形路径?例如,我将两个相交的矩形添加到 GraphicsPath 中。我只想绘制所得图形路径的轮廓。

请注意,我不想填充该区域,我只想绘制轮廓。

例子: https://i.sstatic.net/IAVft.png

How does one outline a graphicspath using GDI+? For example, I add two intersecting rectangles to a GraphicsPath. I want to draw the outline of this resulting graphicspath only.

Note that I don't want to fill the area, I just want to draw the outline.

Example:
https://i.sstatic.net/IAVft.png

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

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

发布评论

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

评论(2

半窗疏影 2024-08-14 19:29:57

没有有效的方法来绘制大纲。然而,GDI+ 确实有一个名为 GdipWindingModeOutline 的函数可以完全做到这一点。
这里是 MSDN 参考
这段代码可以解决这个问题:

// Declaration required for interop
[DllImport(@"gdiplus.dll")]
public static extern int GdipWindingModeOutline( HandleRef path, IntPtr matrix, float flatness );

void someControl_Paint(object sender, PaintEventArgs e)
{
    // Create a path and add some rectangles to it
    GraphicsPath path = new GraphicsPath();
    path.AddRectangles(rectangles.ToArray());

    // Create a handle that the unmanaged code requires. nativePath private unfortunately
    HandleRef handle = new HandleRef(path, (IntPtr)path.GetType().GetField("nativePath", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(path));
    // Change path so it only contains the outline
    GdipWindingModeOutline(handle, IntPtr.Zero, 0.25F);
    using (Pen outlinePen = new Pen(Color.FromArgb(255, Color.Red), 2))
    {
        g.DrawPath(outlinePen, path);
    }
}

There is no managed way to do the outline. However, GDI+ does have an function called GdipWindingModeOutline that can do exactly this.
Here is the MSDN reference
This code does the trick:

// Declaration required for interop
[DllImport(@"gdiplus.dll")]
public static extern int GdipWindingModeOutline( HandleRef path, IntPtr matrix, float flatness );

void someControl_Paint(object sender, PaintEventArgs e)
{
    // Create a path and add some rectangles to it
    GraphicsPath path = new GraphicsPath();
    path.AddRectangles(rectangles.ToArray());

    // Create a handle that the unmanaged code requires. nativePath private unfortunately
    HandleRef handle = new HandleRef(path, (IntPtr)path.GetType().GetField("nativePath", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(path));
    // Change path so it only contains the outline
    GdipWindingModeOutline(handle, IntPtr.Zero, 0.25F);
    using (Pen outlinePen = new Pen(Color.FromArgb(255, Color.Red), 2))
    {
        g.DrawPath(outlinePen, path);
    }
}
魂ガ小子 2024-08-14 19:29:57

我自己无法尝试,但我认为您应该使用形状的交叉点创建一个 Region 对象,然后使用 FrameRgn API。
这是它的 pinvoke 签名

[DllImport("gdi32.dll")]
static extern bool FrameRgn(
    IntPtr hdc, 
    IntPtr hrgn, 
    IntPtr hbr, 
    int nWidth,
    int nHeight);

PS:如果有效,请发布您的解决方案,我很好奇:)

I cannot try it myself, but I think you should create a Region object with your shapes' intersections and then use the FrameRgn API.
Here is its pinvoke signature:

[DllImport("gdi32.dll")]
static extern bool FrameRgn(
    IntPtr hdc, 
    IntPtr hrgn, 
    IntPtr hbr, 
    int nWidth,
    int nHeight);

P.S: please post your solution if this works, I'm curious :)

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