Draw line 1


   

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


   

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

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


   



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

           
          


Graphics Properties


   

/*
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.ComponentModel;
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.ComponentModel.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));

      
    }
    }
}



           
          


Line Joins: Miter, Bevel, Round, MiterClipped

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

class LineJoins: Form
{
public static void Main()
{
Application.Run(new LineJoins());
}
public LineJoins()
{
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)
{
Pen penNarrow = new Pen(clr);
Pen penWide = new Pen(Color.Gray, cx / 16);
Point[] apt = { new Point(1 * cx / 32, 1 * cy / 8),
new Point(4 * cx / 32, 6 * cy / 8),
new Point(7 * cx / 32, 1 * cy / 8) };

for (int i = 0; i < 4; i++) { penWide.LineJoin = (LineJoin) i; grfx.DrawLines(penWide, apt); grfx.DrawLines(penNarrow, apt); grfx.TranslateTransform(cx / 4, 0); } } } [/csharp]

Line Join style: Bevel


   



  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;
      g.SmoothingMode = SmoothingMode.AntiAlias;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      Pen p = new Pen(Color.Black, 10);
      p.LineJoin = LineJoin.Bevel;
      e.Graphics.DrawRectangle(p, 20, 20, 200, 200);
      p.Dispose();
    }

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