简体   繁体   English

在.Net中概述GDI +的路径

[英]Outline a path with GDI+ in .Net

How does one outline a graphicspath using GDI+? 如何使用GDI +概述图形路径? For example, I add two intersecting rectangles to a GraphicsPath. 例如,我将两个相交的矩形添加到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: 例: http://i.stack.imgur.com/IAVft.png

There is no managed way to do the outline. 没有可管理的方式来做大纲。 However, GDI+ does have an function called GdipWindingModeOutline that can do exactly this. 但是,GDI +确实有一个名为GdipWindingModeOutline的函数可以做到这一点。 Here is the MSDN reference This code does the trick: 这是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);
    }
}

I cannot try it myself, but I think you should create a Region object with your shapes' intersections and then use the FrameRgn API. 我不能自己尝试,但我认为您应该使用形状的交叉点创建一个Region对象,然后使用FrameRgn API。
Here is its pinvoke signature : 这是它的pinvoke签名

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

PS: please post your solution if this works, I'm curious :) PS:请发布你的解决方案,如果这样,我很好奇:)

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

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