GDI+基础知识——各种画笔线型

来源:互联网 发布:如何成为淘宝达人步骤 编辑:程序博客网 时间:2024/05/18 12:41

效果:


  //画笔的线型演示
  private void DashStyle_Custom_Click(object sender,EventArgs e)
  {
   using(Graphics graphics = this.CreateGraphics())
   {
    graphics.Clear(Color.White);

    Pen pen = new Pen(Color.Blue, 9);
    //设置文本输出对齐方式及字体
    StringFormat fmt = new StringFormat();
    fmt.Alignment = StringAlignment.Near;
    fmt.LineAlignment = StringAlignment.Center;

    //字体
    Font font = new Font("Arial",20);
    //Font font = new Font("Arial",20,FontStyle.Bold);
    SolidBrush sBrush = new SolidBrush(Color.Black);

    // iLineHeight:可调整行距
    int iLineHeight = 40;
    graphics.TranslateTransform(0,iLineHeight);

    int i = 0;
    //分别使用常见的五种线型绘制直线
   
    for(;i<5;i++)
    {
     //设置线型
     pen.DashStyle = (DashStyle)i;
     graphics.DrawLine(pen, 10, iLineHeight * i, 260, iLineHeight * i);
    
     //输出当前线型的名称
     graphics.DrawString(pen.DashStyle.ToString(),
      font,sBrush,new Point(260,iLineHeight *i),fmt);
    }

    //使用自定义义线型
    float[] dashVals =  {
          3f,   // 线长5个像素
          0.05f,   // 间断2个像素
          1.0f,  // 线长15个像素
          2f,
         }; 
   /*
    //间隔空圆点
    float[] dashVals =  {
          1f,   // 线长5个像素
          1f,   // 间断2个像素
          //1.0f,  // 线长15个像素
          //1f,
    }; 
   */
    pen.DashPattern = dashVals;

    pen.DashCap  =  DashCap.Round;
    pen.StartCap =  LineCap.RoundAnchor;
    pen.EndCap  =  LineCap.ArrowAnchor;

    pen.Color =  Color.Red;
    //注意自定义画笔的线型时,画出的实际值为自定义像素*画笔的宽度
    //因为默认自定义画笔的宽度为1.
    pen.Width = 12;
    graphics.DrawLine(pen,10,iLineHeight*i,550,iLineHeight*i);
    graphics.DrawString(
     pen.DashStyle.ToString(),
     font,
     sBrush,
     new Point(550,iLineHeight*i),fmt
     );
   }
  }