Line style

image_pdfimage_print



   

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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.ComponentModel.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;
   //&#039;   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.CompoundArray=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();
    }

    }
}


           
          


PenProps-c.zip( 5 k)

Draw line 2

image_pdfimage_print



   

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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.ComponentModel.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();
    }
                          


    }
}



           
          


MyPen-C.zip( 5 k)

Draw a line

image_pdfimage_print


   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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.ComponentModel.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();
    }

  }
}



           
          


Draw line 1

image_pdfimage_print


   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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.ComponentModel.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 );
      }

    }

    }
}


           
          


illustrates the use of Pens and Lines

image_pdfimage_print


   

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

  public class Example21_1 : System.Windows.Forms.Form
  {
    private System.ComponentModel.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);
    }
  }


           
          


Draw radiation lines

image_pdfimage_print

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

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) {
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.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } static void Main() { Application.Run(new Form1()); } } [/csharp]

Simplest code to draw a line

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 = "";
      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();
    }
  }