C# GDI+编程(四)

来源:互联网 发布:免费房产中介软件 编辑:程序博客网 时间:2024/05/29 17:58

3.弧线

DGI+中使用DrawArc方法完成圆弧、圆、椭圆弧、椭圆的绘制,可用的方法如下:
public void DrawArc (Pen pen,Rectangle rect,float startAngle,float sweepAngle)
public void DrawArc (Pen pen,RectangleF rect,float startAngle,float sweepAngle)
public void DrawArc (Pen pen,int x,int y,int width,int height,int startAngle,int sweepAngle)
public void DrawArc (Pen pen,float x,float y,float width,float height,float startAngle,float sweepAngle)
这里就第一种方法的参数做一个说明,其他方法的具体使用方法请参考MSDN。
Pen:画笔。
rect:定义弧线的矩形,通过对矩形的控制,将决定是圆弧还是椭圆弧。
startAngle:从X轴到弧线的起始点沿顺时针方向度量的角(单位为度)。
sweepAngle:从StartAngle参数到弧线的结束点沿顺时针方向度量的角(单位为度)。
一个绘制圆弧和圆的例子如下,注意矩形的长和宽:

private void button1_Click(object sender, EventArgs e)
{
   Graphics gp = this.CreateGraphics();
   //绘制红色圆弧
   Pen pen = new Pen(Color.Red);
   Rectangle rect = new Rectangle(1010150150);
   gp.DrawArc(pen, rect, 45180);         
   //绘制蓝色的圆
   pen = new Pen(Color.Blue);
   rect = new Rectangle(5050100100);
   gp.DrawArc(pen, rect, 0360);
   gp.Dispose();
}
运行效果如下:

C GDI+编程(四) - Lemniscate - 信息,灵感,创新

 如果想绘制长轴和短轴分别是200,150的椭圆弧和椭圆,只需做简单的修改:

private void button1_Click(object sender, EventArgs e)
{
   Graphics gp = this.CreateGraphics();
   //绘制红色椭圆弧
   Pen pen = new Pen(Color.Red);
   Rectangle rect = new Rectangle(1010200150);
   gp.DrawArc(pen, rect, 45180);         
   //绘制蓝色的椭圆
   pen = new Pen(Color.Blue);
   rect = new Rectangle(5050200150);
   gp.DrawArc(pen, rect, 0360);
   gp.Dispose();
}
程序运行结果如下:

C GDI+编程(四) - Lemniscate - 信息,灵感,创新

 另外,绘制圆形和椭圆形,还有DrawEllipse方法,其实该方法就是将DrawArc中最后两个参数分别设置为0和360,这里就不赘述了。
原创粉丝点击