Csharp/C Sharp/2D Graphics/Line

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Draw a line

<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace GraphicsObject_c {

   /// <summary>
   /// Summary description for GraphicsObject.
   /// </summary>
 public class GraphicsObject : System.Windows.Forms.Form
 {
   private System.Windows.Forms.PictureBox P1;
   private System.Windows.Forms.Panel Panel1;
   private System.Windows.Forms.Button B1;
   /// <summary>
   /// Required designer variable.
   /// </summary>
   private System.ruponentModel.Container components = null;
   public GraphicsObject()
   {
     //
     // Required for Windows Form Designer support
     //
     InitializeComponent();
     this.P1.Paint += new System.Windows.Forms.PaintEventHandler
       (this.AllPaint);
     this.Panel1.Paint += new System.Windows.Forms.PaintEventHandler
       (this.AllPaint);
     this.B1.Paint += new System.Windows.Forms.PaintEventHandler
       (this.AllPaint);
     
   }
   /// <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()
   {
     this.P1 = new System.Windows.Forms.PictureBox();
     this.Panel1 = new System.Windows.Forms.Panel();
     this.B1 = new System.Windows.Forms.Button();
     this.SuspendLayout();
     // 
     // P1
     // 
     this.P1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
     this.P1.Location = new System.Drawing.Point(192, 16);
     this.P1.Name = "P1";
     this.P1.Size = new System.Drawing.Size(152, 168);
     this.P1.TabIndex = 0;
     this.P1.TabStop = false;
     this.P1.Click += new System.EventHandler(this.P1_Click);
     // 
     // Panel1
     // 
     this.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
     this.Panel1.Location = new System.Drawing.Point(16, 168);
     this.Panel1.Name = "Panel1";
     this.Panel1.Size = new System.Drawing.Size(120, 120);
     this.Panel1.TabIndex = 2;
     // 
     // B1
     // 
     this.B1.Location = new System.Drawing.Point(192, 216);
     this.B1.Name = "B1";
     this.B1.Size = new System.Drawing.Size(152, 120);
     this.B1.TabIndex = 3;
     this.B1.Text = "button1";
     this.B1.Click += new System.EventHandler(this.B1_Click);
     // 
     // GraphicsObject
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(424, 373);
     this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                 this.B1,
                                                                 this.Panel1,
                                                                 this.P1});
     this.Name = "GraphicsObject";
     this.Text = "GraphicsObject";
     this.Load += new System.EventHandler(this.GraphicsObject_Load);
     this.ResumeLayout(false);
   }
       #endregion
   /// <summary>
   /// The main entry point for the application.
   /// </summary>
   [STAThread]
   static void Main() 
   {
     Application.Run(new GraphicsObject());
   }
   private void GraphicsObject_Load(object sender, System.EventArgs e)
   {
   }
   protected override void OnPaint( PaintEventArgs e )
   {
     Graphics G = e.Graphics;
     G.DrawLine(Pens.Black,20,20,100,100);
     base.OnPaint(e);
   }

//----------------------------------------------------------------------------

     void P1Paint( object sender,PaintEventArgs e )
     {
         Graphics G = e.Graphics;
         G.DrawLine(Pens.Black,20,20,100,100);
         base.OnPaint(e);
     }
   void Panel1Paint( object sender,PaintEventArgs e )
   {
     Graphics G = e.Graphics;
     G.DrawLine(Pens.Black,20,20,100,100);
     base.OnPaint(e);
   }
   void ButtonPaint( object sender,PaintEventArgs e )
   {
     Graphics G = e.Graphics;
     G.DrawLine(Pens.Black,20,20,100,100);
     base.OnPaint(e);
   }
   void AllPaint( object sender, PaintEventArgs e )
   {
     Graphics G = e.Graphics;
     if ( sender.GetType() == Panel1.GetType() )
       if ( ((Panel)sender).Name == "Panel1" )
         G.DrawLine(Pens.Red,20,20,100,100);
     if ( sender.GetType() == B1.GetType() )
       if ( ((Button)sender).Name == "B1" )
         G.DrawLine(Pens.Green,20,20,100,100);
     if ( sender.GetType() == P1.GetType() )
       if ( ((PictureBox)sender).Name == "P1" )
         G.DrawLine(Pens.Blue,20,20,100,100);
     base.OnPaint(e);
   }
   private void B1_Click(object sender, System.EventArgs e)
   {
     Graphics G;
     Graphics G2;
     G = this.CreateGraphics();
     G.DrawLine ( new Pen(Color.DarkMagenta,10),50,10,50,100 );
     G2 = Graphics.FromHwnd(this.Handle);
     G2.DrawLine(new Pen(Color.DarkCyan, 10), 70, 10, 70, 100);
     // Create new graphics object using handle to device context.
     Graphics G3  = Graphics.FromHdc(G2.GetHdc());
     G3.DrawLine(new Pen(Color.Black, 10), 85, 10, 85, 100);
     G3.Dispose();
     G.Dispose();
    // G2.Dispose(); 
   }
   private void P1_Click(object sender, System.EventArgs e)
   {
     Image img = Image.FromFile("crane.jpg");
     Graphics G = Graphics.FromImage(img);
     G.DrawLine(new Pen(Color.Aquamarine, 10), 0,(int)(img.Height / 2), 
                                                 (int)(img.Width), 
                                                 (int)(img.Height / 2));
     P1.Image=img;
     G.Dispose();
   }
 }

}


      </source>


Draw a line 2

<source lang="csharp"> /* GDI+ Programming in C# and VB .NET by Nick Symmonds Publisher: Apress ISBN: 159059035X

  • /

using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace PenCaps_c {

   /// <summary>
   /// Summary description for PenCaps.
   /// </summary>
   public class PenCaps2 : System.Windows.Forms.Form
   {
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ruponentModel.Container components = null;
       public PenCaps2()
       {
           //
           // 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()
       {
     // 
     // PenCaps
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(440, 405);
     this.Name = "PenCaps";
     this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
     this.Text = "PenCaps";
     this.Load += new System.EventHandler(this.PenCaps_Load);
   }
       #endregion
       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       [STAThread]
       static void Main() 
       {
           Application.Run(new PenCaps2());
       }
   private void PenCaps_Load(object sender, System.EventArgs e)
   {
   
   }
   protected override void OnPaint ( PaintEventArgs e )
   {
     Graphics G = e.Graphics;
     Point[] PtsA = { new Point(10, 10), 
                      new Point(150, 150), 
                      new Point(400, 10) };
     Point[] PtsB = { new Point(10, 40), 
                      new Point(150, 180), 
                      new Point(400, 40) };
     Point[] PtsC = { new Point(10, 70), 
                      new Point(150, 210), 
                      new Point(400, 70) };
     Point[] PtsD = { new Point(10, 100), 
                      new Point(150, 240), 
                      new Point(400, 100) };
     Pen P = new Pen(Color.Blue, 10);
     G.SmoothingMode=SmoothingMode.AntiAlias;
     P.LineJoin=LineJoin.Bevel;
     G.DrawLines(P, PtsA);
     P.LineJoin=LineJoin.Miter;
     G.DrawLines(P, PtsB);
     P.LineJoin=LineJoin.MiterClipped;
     G.DrawLines(P, PtsC);
     P.LineJoin=LineJoin.Round;
     G.DrawLines(P, PtsD);
   }
   }

}


      </source>


Draw line 1

<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace Finally_c {

   /// <summary>
   /// Summary description for Finally_c.
   /// </summary>
   public class Finally_c : System.Windows.Forms.Form
   {
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ruponentModel.Container components = null;
       public Finally_c()
       {
           //
           // 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()
       {
             // 
             // Finally_c
             // 
             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
             this.ClientSize = new System.Drawing.Size(292, 273);
             this.Name = "Finally_c";
             this.Text = "Finally_c";
             this.Load += new System.EventHandler(this.Finally_c_Load);
       }   
       #endregion
       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       [STAThread]
       static void Main() 
       {
           Application.Run(new Finally_c());
       }
   private void Finally_c_Load(object sender, System.EventArgs e)
   {
   
   }
   protected override void OnPaint ( PaintEventArgs e )
   {
     Pen P = new Pen(Color.Azure);
     try
     {
       e.Graphics.DrawLine(P, 10, 10, 100, 100 );
     }
     finally
     {
       P.Dispose();
     }
     using (Pen P2 = new Pen(Color.Black))
     {
       e.Graphics.DrawLine(P2, 100, 100, 200, 200 );
     }
   }
   }

}


      </source>


Draw line 2

<source lang="csharp"> /* GDI+ Programming in C# and VB .NET by Nick Symmonds Publisher: Apress ISBN: 159059035X

  • /

using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace MyPen_C {

   /// <summary>
   /// Summary description for MyPen.
   /// </summary>
   public class MyPen : System.Windows.Forms.Form
   {
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ruponentModel.Container components = null;
       public MyPen()
       {
           //
           // Required for Windows Form Designer support
           //
           InitializeComponent();
           this.BackColor=Color.Black;
           //
           // 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()
       {
     // 
     // MyPen
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(720, 421);
     this.Name = "MyPen";
     this.Text = "MyPen";
     this.Load += new System.EventHandler(this.MyPen_Load);
   }
       #endregion
       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       [STAThread]
       static void Main() 
       {
           Application.Run(new MyPen());
       }
   private void MyPen_Load(object sender, System.EventArgs e)
   {
   
   }
   protected override void OnPaint(PaintEventArgs e) 
   {
     Graphics G  = e.Graphics;
     Image Stripe  = new Bitmap("colorbars.jpg");
     TextureBrush B1  = new TextureBrush(Stripe);
     SolidBrush B2 =new SolidBrush(Color.Aquamarine);
     Pen P1;
     P1 =new Pen(B1, 10);
     G.DrawLine(P1, 20, 20, this.Width - 40, this.Height - 40);
     System.Threading.Thread.Sleep(1000);
     P1 =new Pen(B2, 10);
     G.DrawLine(P1, 20, 20, this.Width - 40, this.Height - 40);
     System.Threading.Thread.Sleep(1000);
     P1 = new Pen(Color.BlanchedAlmond, 10);
     G.DrawLine(P1, 20, 20, this.Width - 40, this.Height - 40);
     //Reclaim memory
     B1.Dispose();
     B2.Dispose();
     P1.Dispose();
   }
                         
   }

}


      </source>

<A href="http://www.nfex.ru/Code/CSharpDownload/MyPen-C.zip">MyPen-C.zip( 5 k)</a>


Draw line 3

<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class TestGDI5 : System.Windows.Forms.Form{

   //in order to paint something OnPaint method needs to be overridden
   
   protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe) {
       //OnPaint method is a member of Form class 
       //The following call sends pe to an event listener Graphics
       base.OnPaint(pe);
       
       
       Graphics g = pe.Graphics ;
       Pen pn = new Pen( Color.Blue );
       // Rectangle rect = new Rectangle(50, 50, 200, 100);
       Point pt1 = new Point( 30, 30);
       Point pt2 = new Point( 110, 100);
       g.DrawLine( pn, pt1, pt2 ); 
   }
   public static void Main() {
       System.Windows.Forms.Application.Run(new TestGDI5());//display form
   }

}


      </source>


Draw radiation lines

<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form{

   private System.ruponentModel.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) {
       Graphics g = e.Graphics;
       using (Pen blackPen = new Pen(Color.Black, 1)) 
        {
           if (ClientRectangle.Height/10>0)
           {
              for (int y = 0; y < ClientRectangle.Height;
                 y += ClientRectangle.Height / 10)
              {
                 g.DrawLine(blackPen, new Point(0, 0),
                    new Point(ClientRectangle.Width, y));
              }
           }
        }
   }
   private void InitializeComponent()
 {
   this.ruponents = new System.ruponentModel.Container();
   this.Size = new System.Drawing.Size(300,300);
   this.Text = "Form1";
 }
   
   static void Main() {
       Application.Run(new Form1());
 }

}


      </source>


Graphics Properties

<source lang="csharp"> /* GDI+ Programming in C# and VB .NET by Nick Symmonds Publisher: Apress ISBN: 159059035X

  • /

using System; using System.Drawing; using System.Drawing.Text; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace GraphicsProps_c {

   /// <summary>
   /// Summary description for GraphicsProps.
   /// </summary>
   public class GraphicsProps : System.Windows.Forms.Form
   {
   private System.Windows.Forms.Button B0;
   private System.Windows.Forms.Button B1;
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ruponentModel.Container components = null;
       public GraphicsProps()
       {
           //
           // 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()
       {
     this.B0 = new System.Windows.Forms.Button();
     this.B1 = new System.Windows.Forms.Button();
     this.SuspendLayout();
     // 
     // B0
     // 
     this.B0.Location = new System.Drawing.Point(16, 240);
     this.B0.Name = "B0";
     this.B0.Size = new System.Drawing.Size(40, 24);
     this.B0.TabIndex = 0;
     this.B0.Text = "B0";
     this.B0.Click += new System.EventHandler(this.B0_Click);
     // 
     // B1
     // 
     this.B1.Location = new System.Drawing.Point(72, 240);
     this.B1.Name = "B1";
     this.B1.Size = new System.Drawing.Size(40, 24);
     this.B1.TabIndex = 1;
     this.B1.Text = "B1";
     this.B1.Click += new System.EventHandler(this.B1_Click);
     // 
     // GraphicsProps
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(292, 273);
     this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                 this.B1,
                                                                 this.B0});
     this.Name = "GraphicsProps";
     this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
     this.Text = "GraphicsProps";
     this.Load += new System.EventHandler(this.GraphicsProps_Load);
     this.ResumeLayout(false);
   }
       #endregion
       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       [STAThread]
       static void Main() 
       {
           Application.Run(new GraphicsProps());
       }
   private void GraphicsProps_Load(object sender, System.EventArgs e)
   {
   }
   protected override void OnPaint ( PaintEventArgs e )
   {
     Graphics G = e.Graphics;
     G.Clear(Color.Bisque);
     //Change one of the Graphics attributes and save state
     G.SmoothingMode=SmoothingMode.AntiAlias;
     GraphicsState OldG = G.Save();
     //Restore the attribute and draw a line
     G.SmoothingMode=SmoothingMode.Default;
     G.DrawLine(new Pen(Color.DarkMagenta, 20), 10, 50, 
                               (int)(this.Width - 10), 140);
     //Restore the old Graphics state and draw another line
     G.Restore(OldG);
     G.DrawLine(new Pen(Color.DarkMagenta, 20), 10, 100, 
                               (int)(this.Width - 10), 190);
     G.Dispose();
   }
   private void B0_Click(object sender, System.EventArgs e)
   {
     BeginContainerNoArg(this.CreateGraphics());
   }
   private void B1_Click(object sender, System.EventArgs e)
   {
    // BeginContainerIntRectArg(this.CreateGraphics());
    // World2PageXform(this.CreateGraphics());
     RenderText(this.CreateGraphics());
   }
   public void BeginContainerNoArg(Graphics G)
   {
     G.Clear(Color.Bisque);
     //Change one of the attributes of the Graphics object
     //then save the state.
     G.SmoothingMode = SmoothingMode.AntiAlias;
     GraphicsContainer OldG  = G.BeginContainer();
     //Restore the Smoothing mode state and draw a line
     G.SmoothingMode = SmoothingMode.Default;
     G.DrawLine(new Pen(Color.Chocolate, 20), 10, 50, 
                                 (int)(this.Width - 10), 150);
     //Restore the old Graphics state and draw another line
     G.EndContainer(OldG);
     G.DrawLine(new Pen(Color.Chocolate, 20), 10, 100, 
                                 (int)(this.Width - 10), 200);
     G.Dispose();
   }
   public void BeginContainerIntRectArg(Graphics G)
   {
     G.Clear(Color.Bisque);
     // Define transformation for container.
     Rectangle srcRect = new Rectangle(0, 0, 200, 200);
     Rectangle destRect = new Rectangle(0, 0, 100, 100);
     // Begin graphics container.
     GraphicsContainer containerState  = G.BeginContainer(destRect, 
                                               srcRect, GraphicsUnit.Pixel);
     G.DrawLine(new Pen(Color.DarkOrchid, 20), 10, 100, 200, 100);
     G.EndContainer(containerState);
     G.DrawLine(new Pen(Color.DarkOrchid, 20), 10, 100, 200, 100);
     G.Dispose();
   }
   public void World2PageXform(Graphics G)
   {
     int EndX = 1;
     int EndY = 1;
     G.Clear(Color.Azure);
     G.PageUnit=GraphicsUnit.Inch;
     G.TranslateTransform(1, 1);
     G.DrawLine(Pens.Blue, 0, 0, EndX, EndY);
     int Xpix = EndX * (int)G.DpiX;
     int Ypix = EndY * (int)G.DpiY;
   }
   public void RenderText(Graphics G)
   {
     Font F = new Font("Arial", 16);
     SolidBrush B = new SolidBrush(Color.Black);
     G.Clear(Color.Azure);
     G.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
     G.DrawString("SingleBitPerPixel", F, B, new PointF(10, 10));
     G.TextRenderingHint = TextRenderingHint.AntiAlias;
     G.DrawString("AntiAlias default Contrast", F, B, new PointF(10, 60));
     G.TextContrast = 12;
     G.DrawString("AntiAlias Low Contrast", F, B, new PointF(10, 90));
     G.TextContrast = 1;
     G.DrawString("AntiAlias High Contrast", F, B, new PointF(10, 120));
     
   }
   }

}


      </source>


illustrates the use of Pens and Lines

<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;

 public class Example21_1 : System.Windows.Forms.Form
 {
   private System.ruponentModel.Container components = null;
   public Example21_1()
   {
     InitializeComponent();
   }
   protected override void Dispose( bool disposing )
   {
     if( disposing )
     {
       if (components != null) 
       {
         components.Dispose();
       }
     }
     base.Dispose( disposing );
   }
   private void InitializeComponent()
   {
     this.BackColor = System.Drawing.Color.White;
     this.ClientSize = new System.Drawing.Size(400, 400);
     this.Paint += new System.Windows.Forms.PaintEventHandler(this.Example21_1_Paint);
   }
   static void Main() 
   {
     Application.Run(new Example21_1());
   }
   private void Example21_1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
   {
     Graphics g = e.Graphics;
     Pen p = new Pen(Color.Black, 10);
     g.DrawLine(p, 25, 25, 375, 375);
   }
 }


      </source>


Line style

<source lang="csharp"> /* GDI+ Programming in C# and VB .NET by Nick Symmonds Publisher: Apress ISBN: 159059035X

  • /

using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace PenProps_c {

   /// <summary>
   /// Summary description for PenProps.
   /// </summary>
   public class PenProps : System.Windows.Forms.Form
   {
   private System.Windows.Forms.Label lblType;
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ruponentModel.Container components = null;
       public PenProps()
       {
           //
           // 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()
       {
     this.lblType = new System.Windows.Forms.Label();
     this.SuspendLayout();
     // 
     // lblType
     // 
     this.lblType.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
     this.lblType.Location = new System.Drawing.Point(56, 184);
     this.lblType.Name = "lblType";
     this.lblType.Size = new System.Drawing.Size(328, 16);
     this.lblType.TabIndex = 0;
     // 
     // PenProps
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(464, 237);
     this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                 this.lblType});
     this.Name = "PenProps";
     this.Text = "PenProps";
     this.Load += new System.EventHandler(this.PenProps_Load);
     this.ResumeLayout(false);
   }
       #endregion
       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       [STAThread]
       static void Main() 
       {
           Application.Run(new PenProps());
       }
   private void PenProps_Load(object sender, System.EventArgs e)
   {
   
   }
   protected override void OnPaint(PaintEventArgs e) 
   {
     Graphics G  = e.Graphics;

// Pen P1=new Pen(Color.Blue, 10); // // G.DrawLine(P1, 20, this.Height/2, this.Width - 20, this.Height/2); // // //Change color and width // P1.Color=Color.DarkOrange; // P1.Width=5; // G.DrawLine(P1, 20, this.Height/2, this.Width - 20, this.Height/2); // P1.Width=20; // // //Change brush // Pen P2=new Pen(Color.Blue, 10); // G.DrawLine(P2, 20, this.Height/3, this.Width - 20, this.Height/3); // P2.Brush=new TextureBrush(new Bitmap("colorbars.jpg")); // G.DrawLine(P2, 20, this.Height/2, this.Width - 20, this.Height/2); // P1.Dispose();

     Pen P2=new Pen(Color.Blue, 10);
     float[] Pts = {3, 1, 2, 5};
     P2.DashStyle=System.Drawing.Drawing2D.DashStyle.Dash;
     P2.DashPattern=Pts;
  //"   P2.DashOffset=40;
     P2.DashCap=System.Drawing.Drawing2D.DashCap.Triangle;
     P2.StartCap = System.Drawing.Drawing2D.LineCap.Round;
     P2.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
     G.DrawLine(P2, 20, this.Height/2, this.Width - 20, this.Height/2);
     //pentype
     G.Clear(Color.Khaki);
     Pen P3=new Pen(Color.Blue, 10);
     P3.Brush=new TextureBrush(new Bitmap("colorbars.jpg"));
     G.DrawLine(P3, 20, this.Height/2, this.Width - 20, this.Height/2);
     lblType.Text = "PenType is " + P3.PenType.ToString();
     //Compoundarray
     P3.Dispose();
     G.Clear(Color.Khaki);
     Single[] lines = {0.0f, 0.1f, 0.9f, 1.0f};
     P3=new Pen(Color.Blue, 20);
     P3.rupoundArray=lines;
     G.DrawLine(P3, 20, this.Height/2, this.Width - 20, this.Height/2);
     //pens class
     P3.Dispose();
     G.Clear(Color.Khaki);
     P3 = Pens.LightSlateGray;
     G.DrawLine(P3, 20, this.Height/2, this.Width - 20, this.Height/2);
     G.DrawLine(Pens.Violet, 20, this.Height/2, 
               this.Width - 20, this.Height/2);
     if (P2 != null)
       P2.Dispose();
   }
   }

}


      </source>

<A href="http://www.nfex.ru/Code/CSharpDownload/PenProps-c.zip">PenProps-c.zip( 5 k)</a>


Simplest code to draw a line

<source lang="csharp">

 using System;
 using System.Drawing;
 using System.Drawing.Drawing2D;
 using System.Collections;
 using System.ruponentModel;
 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 = "";
     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);
     Pen p = new Pen(Color.Black);
     g.DrawLine(p, 0, 0, 100, 100);
     p.Dispose();
   }
   private void Form1_Resize(object sender, System.EventArgs e)
   {
     Invalidate();
   }
 }
          
      </source>


Simplest Coordinate

<source lang="csharp"> /* GDI+ Programming in C# and VB .NET by Nick Symmonds Publisher: Apress ISBN: 159059035X

  • /

using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace CustomCap_c {

   /// <summary>
   /// Summary description for CustomCap.
   /// </summary>
   public class CustomCap : System.Windows.Forms.Form
   {
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ruponentModel.Container components = null;
       public CustomCap()
       {
           //
           // 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()
       {
     // 
     // CustomCap
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(292, 273);
     this.Name = "CustomCap";
     this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
     this.Text = "CustomCap";
     this.Load += new System.EventHandler(this.CustomCap_Load);
   }
       #endregion
       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       [STAThread]
       static void Main() 
       {
           Application.Run(new CustomCap());
       }
   private void CustomCap_Load(object sender, System.EventArgs e)
   {
   }
   protected override void OnPaint ( PaintEventArgs e )
   {
     Graphics G = e.Graphics;
     Pen P = new Pen(Color.Blue, 1 );
     Point[] Pts = { new Point( 10, 10 ),
                     new Point( 15, 10 ),
                     new Point( 20, 15 ),
                     new Point( 20, 20 ),
                     new Point( 15, 25 ),
                     new Point( 10, 25 ),
                     new Point( 5, 20 ),
                     new Point( 5, 15 ),
                     new Point( 10, 10 )};
     GraphicsPath Path = new GraphicsPath();
     Path.AddLines (Pts);
     G.SmoothingMode=SmoothingMode.AntiAlias;
     CustomLineCap Lc = new CustomLineCap( null, Path );
     Lc.BaseInset=0;
     Lc.WidthScale=1;
     Lc.StrokeJoin=LineJoin.Miter;
     P.CustomEndCap = Lc;
     P.CustomStartCap=Lc;
     G.DrawLine ( P, 50, 150, 200, 150 );
     G.DrawLine ( P, 150, 50, 150, 200 );
     Lc.Dispose();
     Path.Dispose();
     P.Dispose();
   }
   }

}


      </source>