Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outline a path with GDI+ in .Net

Tags:

c#

.net

gdi+

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: http://i.stack.imgur.com/IAVft.png

like image 606
Dodgyrabbit Avatar asked Oct 20 '09 03:10

Dodgyrabbit


1 Answers

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);
    }
}
like image 74
Dodgyrabbit Avatar answered Oct 23 '22 13:10

Dodgyrabbit