Scribble with Path

image_pdfimage_print
   
 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class ScribbleWithPath: Form
{
     GraphicsPath path= new GraphicsPath();
     bool         bTracking;
     Point        ptLast;
   
     public static void Main()
     {
          Application.Run(new ScribbleWithPath());
     }
     protected override void OnMouseDown(MouseEventArgs mea)
     {
          if (mea.Button != MouseButtons.Left)
               return;
   
          ptLast = new Point(mea.X, mea.Y);
          bTracking = true;
   
          path.StartFigure();
     }
     protected override void OnMouseMove(MouseEventArgs mea)
     {
          if (!bTracking)
               return;
   
          Point ptNew = new Point(mea.X, mea.Y);
          
          Graphics grfx = CreateGraphics();
          grfx.DrawLine(new Pen(ForeColor), ptLast, ptNew);
          grfx.Dispose();
   
          path.AddLine(ptLast, ptNew);
   
          ptLast = ptNew;
     }
     protected override void OnMouseUp(MouseEventArgs mea)
     {
          bTracking = false;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          pea.Graphics.DrawPath(new Pen(ForeColor), path);
     }
}

    


Widen a Path

image_pdfimage_print

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

class WidenPath: Form
{
GraphicsPath path= new GraphicsPath();

public static void Main()
{
Application.Run(new WidenPath());
}
public WidenPath()
{
ResizeRedraw = true;

path.AddLines(new Point[] { new Point(20, 10),
new Point(50, 50),
new Point(80, 10) });

path.AddPolygon(new Point[] { new Point(20, 30),
new Point(50, 70),
new Point(80, 30) });
}
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)
{
grfx.ScaleTransform(cx / 300f, cy / 200f);

for (int i = 0; i < 6; i++) { GraphicsPath pathClone = (GraphicsPath) path.Clone(); Matrix matrix = new Matrix(); Pen penThin = new Pen(clr, 1); Pen penThick = new Pen(clr, 5); Pen penWiden = new Pen(clr, 7.5f); Brush brush = new SolidBrush(clr); matrix.Translate((i % 3) * 100, (i / 3) * 100); if (i < 3) pathClone.Transform(matrix); else pathClone.Widen(penWiden, matrix); switch (i % 3) { case 0: grfx.DrawPath(penThin, pathClone); break; case 1: grfx.DrawPath(penThick, pathClone); break; case 2: grfx.FillPath(brush, pathClone); break; } } } } [/csharp]

Add Ellipse to path

image_pdfimage_print

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

class Clover: Form
{
public static void Main()
{
Application.Run(new Clover());
}
public Clover()
{
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)
{
GraphicsPath path = new GraphicsPath();

path.AddEllipse(0, cy / 3, cx / 2, cy / 3); // Left

grfx.SetClip(path);
grfx.TranslateTransform(cx / 2, cy / 2);

Pen pen = new Pen(clr);
float fRadius = (float) Math.Sqrt(Math.Pow(cx / 2, 2) +
Math.Pow(cy / 2, 2));

for (float fAngle = 0; fAngle < (float) Math.PI * 2; fAngle += (float) Math.PI / 180) { grfx.DrawLine(pen, 0, 0, fRadius * (float) Math.Cos(fAngle), -fRadius * (float) Math.Sin(fAngle)); } } } [/csharp]

Add lines to Path

image_pdfimage_print


   


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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Pen Cap App";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      GraphicsPath gp = new GraphicsPath();

      gp.AddLine(10, 10, 110, 15);
      gp.AddLine(110, 15, 100, 96);
      gp.AddLine(100, 96, 15, 110);
      gp.CloseFigure();

      g.FillRectangle(Brushes.White, this.ClientRectangle);
      g.DrawPath(Pens.Black, gp);
      gp.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


           
          


Fill path and draw path

image_pdfimage_print


   


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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      GraphicsPath gp = new GraphicsPath();

      // Create an open figure
      gp.AddLine(10, 10, 10, 50);
      gp.AddLine(10, 50, 50, 50);

      // Start a new figure
      gp.StartFigure();
      gp.AddLine(60, 10, 60, 50);
      gp.AddLine(60, 50, 100, 50);
      gp.AddLine(100, 50, 100, 10);
      gp.CloseFigure();

      gp.AddEllipse(new Rectangle(110, 10, 40, 40));

      g.FillPath(Brushes.Orange, gp);
      g.DrawPath(Pens.Black, gp);
      gp.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


           
          


Add closed figures to GraphicsPath

image_pdfimage_print


   



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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      GraphicsPath gp = new GraphicsPath();

      // Create a figure
      gp.AddRectangle(new Rectangle(10, 50, 80, 20));

      // Create another figure
      gp.AddEllipse(50, 10, 20, 80);
      g.DrawPath(Pens.Black, gp);
      gp.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }

           
          


GraphicsPath starts two figures and close them

image_pdfimage_print


   


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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      GraphicsPath gp = new GraphicsPath();

      // Create a figure
      gp.AddLine(10, 10, 10, 50);
      gp.AddBezier(10, 50, 100, 55, 25, 70, 30, 70);
      gp.AddLine(30, 70, 60, 70);

      // Create another figure
      gp.StartFigure();
      gp.AddLine(60, 110, 40, 160);
      gp.AddLine(40, 160, 60, 180);

      // Close all figures
      gp.CloseAllFigures();

      // Draw the path
      g.DrawPath(Pens.Black, gp);

      gp.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }