Draw path


   

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 System.ComponentModel.Container components = null;

  public Form1()
  {
    InitializeComponent();
  }

  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }

    protected override void OnPaint (PaintEventArgs e) {
         GraphicsPath path;
         path = new GraphicsPath(new Point[]{ new Point(10, 10),
                                               new Point(50, 10),
                                               new Point(20, 150),
                                               new Point(10, 50),
                                               new Point(200, 160)}, 
            new byte[] {(byte)PathPointType.Start,
                          (byte)PathPointType.Line,
                          (byte)PathPointType.Line,
                          (byte)PathPointType.Line,
                          (byte)PathPointType.Line });
         e.Graphics.DrawPath(Pens.Black, path);
    }
  private void InitializeComponent() {
      this.components = new System.ComponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "Form1";
  }
  static void Main() {
    Application.Run(new Form1());
  }
}



           
          


Graphics Path Example


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace GDI_Basics
{
    /// <summary>
    /// Summary description for GraphicsPathExample.
    /// </summary>
    public class GraphicsPathExample : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public GraphicsPathExample()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // GraphicsPathExample
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "GraphicsPathExample";
            this.Text = "GraphicsPathExample";
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.GraphicsPathExample_MouseDown);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.GraphicsPathExample_Paint);

        }
        #endregion


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

        GraphicsPath path;

        private void GraphicsPathExample_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            path = new GraphicsPath();
            path.StartFigure();
            path.AddArc(10, 10, 100, 100, 20, 50);
            path.AddLine(20, 50, 70, 230);
            path.CloseFigure();
            path.AddEllipse(120, 50, 80, 80);
            e.Graphics.FillPath(Brushes.White, path);
            e.Graphics.DrawPath(Pens.Black, path);
        }

        private void GraphicsPathExample_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (path.IsVisible(e.X, e.Y))
            {
                MessageBox.Show("You clicked inside the figure.");
            }

        }
    }
}



           
          


Add Line and Arc to Path

   
 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class LineArcPath: Form
{
     public static void Main()
     {
          Application.Run(new LineArcPath());
     }
     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();
          Pen          pen  = new Pen(clr, 25);
   
          path.AddLine( 25, 100, 125, 100);
          path.AddArc (125,  50, 100, 100, -180, 180);
          path.AddLine(225, 100, 325, 100);
   
          grfx.DrawPath(pen, path);
     }
}

    


Add Bezier to a Path

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

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

float fScale = Math.Min(cx, cy) / 200f;
grfx.TranslateTransform(cx / 2, cy / 2);
grfx.ScaleTransform(fScale, fScale);

GraphicsPath path = new GraphicsPath();

path.AddBezier(new Point( 0, 0), new Point(125, 125),
new Point(75, 15), new Point(600, 0));

for (int i = 0; i < 8; i++) { grfx.FillPath(Brushes.Red, path); grfx.DrawPath(Pens.Black, path); grfx.RotateTransform(360 / 8); } Rectangle rect = new Rectangle(-150, -150, 300, 300); grfx.FillEllipse(Brushes.Yellow, rect); grfx.DrawEllipse(Pens.Black, rect); } } [/csharp]

Scribble with Path

   
 
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

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

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]