Graphics对象表示GDI+绘图表面,是用于创建图形图像的对象,所以要通过GDI+创建绘图,必须先创建Graphics对象,然后才可以使用GDI+的笔、刷等结合颜色、字体等对象进行绘制线条形状、填充区域、显示文本图像等操作。
Graphics对象创建的三种方式
1.Pait事件的PaintEvenlArgs中Graphics对象;
2.从Image创建Graphics对象;
3.用CreateGraphics方法创建Graphics对象。
Pen类位于System.Drawing名称空间中。
使用GDI+中的Pen类来创建钢笔,在创建钢笔时,可以为其定义线条的宽度(width)和颜色color属性
最简单的创建方式为: Pen p=new Pen(color.white,1); 定义一个钢笔p 颜色为白色,1px
//窗体绘图事件 private void Form1_Paint(object sender, PaintEventArgs e) { //创建Graphics对象 Graphics g = CreateGraphics(); //定义钢笔红色,宽度2 Pen p = new Pen(Color.Red,2); //画椭圆,从点0,20-80开始画 确定一个点 400长轴距 241短轴距 g.DrawEllipse(p,20,80,400,241); }