【问题标题】:Outline a path with GDI+ in .Net在 .Net 中使用 GDI+ 勾勒路径
【发布时间】:2010-12-08 05:11:01
【问题描述】:

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

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

示例:

【问题讨论】:

    标签: c# .net gdi+


    【解决方案1】:

    没有管理的方式来做大纲。但是,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);
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      P.S:如果可行,请发布您的解决方案,我很好奇 :)

      【讨论】:

        猜你喜欢
        • 2021-05-16
        • 2022-06-10
        • 1970-01-01
        • 2015-11-11
        • 1970-01-01
        • 2012-07-04
        • 2014-08-23
        • 1970-01-01
        • 2016-02-26
        相关资源
        最近更新 更多