八、使用GDI+同时绘制多种不同图形

使用的函数DrawPath

在绘制直线的时候我们绘制了坐标,我用了多个DrawLine函数堆砌而成,在后来的绘制直线(补充)中和绘制曲线当中都是重复使用DrawLine和DrawBezier函数去实现的。

现在使用DrawPath函数可以一次绘制不同组合的图形。

DrawPath(Pen,GraphicsPath)

GraphicsPath是System.Drawing.Drawing2D命名空间中的一个类,这个类里面有对应GDI+DrawArc,DrawPie,DrawRectangle等等。

绘制三角形,四边形,圆,椭圆,直线,曲线,弧度组合。

private void Form1_Paint(object sender, PaintEventArgs e)
{    
    //创建画板从Paint事件中的直接引用Graphics对象
    Graphics graphics = e.Graphics;

    graphics.Clear(Color.Black);

    //定义画笔
    Pen pen = new Pen(Color.White, 3.0f);
    Pen thickPen = new Pen(Color.White,2.0f);
    Pen thick = new Pen(Color.Red, 2.0f);

    GraphicsPath path = new GraphicsPath();
    path.AddPolygon(new Point[] { new Point(100, 100), new Point(50, 250), new Point(150, 250) });
    path.AddRectangle(new Rectangle(100, 100, 150, 50));
    path.AddEllipse(100, 100, 300, 300);
    path.AddEllipse(100, 100, 200, 100);
    path.AddLine(new Point(100, 50), new Point(300, 400));
    path.AddArc(new Rectangle(150, 150, 200, 220), -30, -90);
    path.AddBezier(new Point(100, 100), new Point(120, 50), new Point(230, 180), new Point(460, 550));
    graphics.DrawPath(pen, path);

}

posted on 2012-05-18 18:56  kiny  阅读(864)  评论(0编辑  收藏  举报