All dash styles

image_pdfimage_print


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Drawing2D;

public class Form1 : Form
{

      public Form1() {
            InitializeComponent();
            
      }
    private void SimpleStyleRenderer_Paint(object sender, PaintEventArgs e)
    {
      Pen myPen = new Pen(Color.Blue, 10);
      int y = 20;
      foreach (DashStyle dash in Enum.GetValues(typeof(DashStyle)))
      {
        myPen.DashStyle = dash;
        e.Graphics.DrawLine(myPen, 20, y, 100, y);
        e.Graphics.DrawString(dash.ToString(), new Font("Tahoma", 8), Brushes.Black, 120, y - 10);
        y += 30;
      }

      y += 10;
      myPen.StartCap = LineCap.Round;
      myPen.EndCap = LineCap.Round;

      foreach (DashStyle dash in System.Enum.GetValues(typeof(DashStyle)))
      {
        myPen.DashStyle = dash;
        e.Graphics.DrawLine(myPen, 20, y, 100, y);
        e.Graphics.DrawString(dash.ToString() + " (with round caps)", new Font("Tahoma", 8), Brushes.Black, 120, y - 10);
        y += 30;
      }

    }


    private void InitializeComponent()
    {
      this.SuspendLayout();
      // 
      // SimpleStyleRenderer
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(384, 353);
      this.Name = "SimpleStyleRenderer";
      this.Text = "SimpleStyleRenderer";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.SimpleStyleRenderer_Paint);
      this.ResumeLayout(false);

    }
      [STAThread]
      static void Main()
      {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
      }

}


           
          


Click on the form to draw curve

image_pdfimage_print


   


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

public class Form1 : System.Windows.Forms.Form{
   private Point[] thePoints;
   private int counter;

   public Form1() {
      Console.WriteLine("click on the form to draw curve");
    
    
      InitializeComponent();
      thePoints = new Point[1000];
      for (int i=0;i<thePoints.Length; i++)
         thePoints&#91;i&#93; = new Point(0,0);
      counter =0;
   }
   protected override void OnPaint (PaintEventArgs e) {
      if (counter==0) 
         return;

      Pen redPen   = new Pen(Color.Red, 3);
      Pen greenPen = new Pen(Color.Red, 4);
      Point&#91;&#93; curvePoints = new Point&#91;counter&#93;;
      
      for (int i=0; i<curvePoints.Length;i++)
      {
         curvePoints&#91;i&#93; = new Point(0,0);
         curvePoints&#91;i&#93;.X = thePoints&#91;i&#93;.X;
         curvePoints&#91;i&#93;.Y = thePoints&#91;i&#93;.Y;
      }
        
      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
      e.Graphics.DrawCurve(greenPen, curvePoints);
   }

   private void InitializeComponent() {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Name = "Form1";
      this.Text = "Form1";
      this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
   }

   static void Main() {
      Application.Run(new Form1());
   }

   private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
      thePoints&#91;counter&#93;.X = e.X;
      thePoints&#91;counter&#93;.Y = e.Y;
      Label l = new Label();
      this.SuspendLayout();

      counter++;
      if (counter>4)
        this.Refresh();
  }
}
           
          


Draw closed curve

image_pdfimage_print


   


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

public class Form1 : System.Windows.Forms.Form{
   private Point[] thePoints;
   private int counter;

   public Form1() {
      Console.WriteLine("click on the form to draw closed curve.");
    
    
      InitializeComponent();
      thePoints = new Point[1000];
      for (int i=0;i<thePoints.Length; i++)
         thePoints&#91;i&#93; = new Point(0,0);
      counter =0;
   }
   protected override void OnPaint (PaintEventArgs e) {
      if (counter==0) 
         return;

      Pen redPen   = new Pen(Color.Red, 3);
      Pen greenPen = new Pen(Color.Red, 4);
      Point&#91;&#93; curvePoints = new Point&#91;counter&#93;;
      
      for (int i=0; i<curvePoints.Length;i++)
      {
         curvePoints&#91;i&#93; = new Point(0,0);
         curvePoints&#91;i&#93;.X = thePoints&#91;i&#93;.X;
         curvePoints&#91;i&#93;.Y = thePoints&#91;i&#93;.Y;
      }
        
      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
      e.Graphics.DrawClosedCurve(greenPen, curvePoints);
   }

   private void InitializeComponent() {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Name = "Form1";
      this.Text = "Form1";
      this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
   }

   static void Main() {
      Application.Run(new Form1());
   }

   private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
      thePoints&#91;counter&#93;.X = e.X;
      thePoints&#91;counter&#93;.Y = e.Y;
      Label l = new Label();
      this.SuspendLayout();

      counter++;
      if (counter>4)
        this.Refresh();
  }
}
           
          


Bezier (Mouse Defines Control Points)

image_pdfimage_print
   
 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class Bezier: Form
{
     protected Point[] apt = new Point[4];
   
     public static void Main()
     {
          Application.Run(new Bezier());
     }
     public Bezier()
     {
          ResizeRedraw = true;
   
          OnResize(EventArgs.Empty);
     }
     protected override void OnResize(EventArgs ea)
     {
          base.OnResize(ea);
   
          int cx = ClientSize.Width;
          int cy = ClientSize.Height;
   
          apt[0] = new Point(    cx / 4,     cy / 2);
          apt[1] = new Point(    cx / 2,     cy / 4);
          apt[2] = new Point(    cx / 2, 3 * cy / 4);
          apt[3] = new Point(3 * cx / 4,     cy / 2);
     }
     protected override void OnMouseDown(MouseEventArgs mea)
     {
          Point pt;
   
          if (mea.Button == MouseButtons.Left)
               pt = apt[1];
   
          else if (mea.Button == MouseButtons.Right)
               pt = apt[2];
   
          else
               return;
   
          Cursor.Position = PointToScreen(pt);
     }
     protected override void OnMouseMove(MouseEventArgs mea)
     {
          if (mea.Button == MouseButtons.Left)
          {
               apt[1] = new Point(mea.X, mea.Y);
               Invalidate();
          }
          else if (mea.Button == MouseButtons.Right)
          {
               apt[2] = new Point(mea.X, mea.Y);
               Invalidate();
          }
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
   
          grfx.DrawBeziers(new Pen(ForeColor), apt);
   
          Pen pen = new Pen(Color.FromArgb(0x80, ForeColor));
   
          grfx.DrawLine(pen, apt[0], apt[1]);
          grfx.DrawLine(pen, apt[2], apt[3]);
     }
}

    


Spiral

image_pdfimage_print

using System;
using System.Drawing;
using System.Windows.Forms;

class Spiral: Form
{
public static void Main()
{
Application.Run(new Spiral());
}
public Spiral()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
const int iNumRevs = 20;
int iNumPoints = iNumRevs * 2 * (cx + cy);
PointF[] aptf = new PointF[iNumPoints];
float fAngle, fScale;

for (int i = 0; i < iNumPoints; i++) { fAngle = (float)(i * 2 * Math.PI /(iNumPoints / iNumRevs)); fScale = 1 - (float)i / iNumPoints; aptf[i].X = (float)(cx / 2 * (1 + fScale * Math.Cos(fAngle))); aptf[i].Y = (float)(cy / 2 * (1 + fScale * Math.Sin(fAngle))); } grfx.DrawLines(new Pen(clr), aptf); } } [/csharp]

Ellipse with DrawLines

image_pdfimage_print

using System;
using System.Drawing;
using System.Windows.Forms;

class PolyEllipse: Form
{
public static void Main()
{
Application.Run(new PolyEllipse());
}
public PolyEllipse()
{
Text = “Ellipse with DrawLines”;
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
int iNum = 2 * (cx + cy);
PointF[] aptf = new PointF[iNum];

for (int i = 0; i < iNum; i++) { double dAng = i * 2 * Math.PI / (iNum - 1); aptf[i].X = (cx - 1) / 2f * (1 + (float)Math.Cos(dAng)); aptf[i].Y = (cy - 1) / 2f * (1 + (float)Math.Sin(dAng)); } grfx.DrawLines(new Pen(clr), aptf); } } [/csharp]

Sine Curve

image_pdfimage_print

using System;
using System.Drawing;
using System.Windows.Forms;

class SineCurve: Form
{
public static void Main()
{
Application.Run(new SineCurve());
}
public SineCurve()
{
Text = “Sine Curve”;
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
PointF[] aptf = new PointF[cx];

for (int i = 0; i < cx; i++) { aptf[i].X = i; aptf[i].Y = cy / 2 * (1 -(float) Math.Sin(i * 2 * Math.PI / (cx - 1))); } grfx.DrawLines(new Pen(clr), aptf); } } [/csharp]