c#绘图和合并

时间:2013-10-18 13:26:42

标签: c# .net drawing cad

我相信它是可能的,但不是那样的。我希望有这样的矩形(一些水平和一些垂直):取第一个,将它放在第二个,当线接触时,创建一个没有内线的“L”。这将是一个基本的CAD绘图。

该项目将使用visual studio 2010 ultimate in c#with framework 4。

创建

如果有人有线索或教程的路径,我会很感激。

谢谢!

编辑:我的尝试。

System.Drawing.Rectangle rectangle1 = new System.Drawing.Rectangle(30, 40, 50, 200);          System.Drawing.Rectangle rectangle2 = new System.Drawing.Rectangle(30, 190, 200, 50);
e.Graphics.DrawRectangle(Pens.Blue, rectangle1);
e.Graphics.DrawRectangle(Pens.GreenYellow, rectangle2);
System.Drawing.Rectangle rectangle3 = System.Drawing.Rectangle.Intersect(rectangle1, rectangle2);
e.Graphics.DrawRectangle(Pens.White, rectangle3);

1 个答案:

答案 0 :(得分:1)

将您的矩形添加到GraphicsPath,然后使用GdipWindingModeOutline()API将其转换为此SO问题中的轮廓。

L-Shaped Outline from GraphicsPath

具体来说,以你的例子:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

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

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Rectangle rectangle1 = new Rectangle(30, 40, 50, 200); 
        Rectangle rectangle2 = new Rectangle(30, 190, 200, 50);

        GraphicsPath gp = new GraphicsPath();
        gp.AddRectangle(rectangle1);
        gp.AddRectangle(rectangle2);

        HandleRef handle = new HandleRef(gp, (IntPtr)gp.GetType().GetField("nativePath", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(gp));
        GdipWindingModeOutline(handle, IntPtr.Zero, 0.25F);

        e.Graphics.DrawPath(Pens.Blue, gp);
    }

}