Two-Point Linear Gradient Brush

   
 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class TwoPointLinearGradientBrush: Form
{
     public static void Main()
     {
          Application.Run(new TwoPointLinearGradientBrush());
     }
     TwoPointLinearGradientBrush()
     {
          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)
     {
          LinearGradientBrush lgbrush = 
               new LinearGradientBrush(
                         new Point(cx / 4, cy / 4), 
                         new Point(3 * cx / 4, 3 * cy / 4),
                         Color.White, Color.Black);
   
          grfx.FillRectangle(lgbrush, 0, 0, cx, cy);
     }
}

    


Draw a line 2


   

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

    }
}


           
          


Simplest Coordinate


   

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

    }
}


           
          


Draw line 3


   

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




           
          


Line style



   

/*
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



   

/*
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


   

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

  }
}