2009-10-20 57 views
4

如何勾畫使用GDI +的圖形路徑?例如,我將兩個相交的矩形添加到GraphicsPath中。我只想畫出這個結果圖形路徑的輪廓。在.Net中概述一條GDI +的路徑

請注意,我不想填寫該區域,我只想繪製輪廓。

例子: http://i.stack.imgur.com/IAVft.png

回答

8

有做輪廓沒有管理辦法。但是,GDI +確實有一個名爲GdipWindingModeOutline的函數,可以完成此操作。 Here is the MSDN reference 此代碼的竅門:

// 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); 
    } 
} 
0

我不能嘗試一下自己,但我想你應該創建你的形狀交叉Region對象,然後使用FrameRgn API。
下面是它的pinvoke signature

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

P.S:請把你的解決方案,如果這個工程,我很好奇:)