Draw a line : Line « 2D Graphics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » 2D Graphics » LineScreenshots 
Draw a line
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 Disposebool disposing )
    {
      ifdisposing )
      {
        if (components != null
        {
          components.Dispose();
        }
      }
      base.Disposedisposing );
    }

        #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(19216);
      this.P1.Name = "P1";
      this.P1.Size = new System.Drawing.Size(152168);
      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(16168);
      this.Panel1.Name = "Panel1";
      this.Panel1.Size = new System.Drawing.Size(120120);
      this.Panel1.TabIndex = 2;
      // 
      // B1
      // 
      this.B1.Location = new System.Drawing.Point(192216);
      this.B1.Name = "B1";
      this.B1.Size = new System.Drawing.Size(152120);
      this.B1.TabIndex = 3;
      this.B1.Text = "button1";
      this.B1.Click += new System.EventHandler(this.B1_Click);
      // 
      // GraphicsObject
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(424373);
      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 OnPaintPaintEventArgs e )
    {
      Graphics G = e.Graphics;

      G.DrawLine(Pens.Black,20,20,100,100);
      base.OnPaint(e);
    }

//----------------------------------------------------------------------------
      void P1Paintobject sender,PaintEventArgs e )
      {
          Graphics G = e.Graphics;

          G.DrawLine(Pens.Black,20,20,100,100);
          base.OnPaint(e);
      }

    void Panel1Paintobject sender,PaintEventArgs e )
    {
      Graphics G = e.Graphics;

      G.DrawLine(Pens.Black,20,20,100,100);
      base.OnPaint(e);
    }

    void ButtonPaintobject sender,PaintEventArgs e )
    {
      Graphics G = e.Graphics;

      G.DrawLine(Pens.Black,20,20,100,100);
      base.OnPaint(e);
    }

    void AllPaintobject 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)701070100);

      // Create new graphics object using handle to device context.
      Graphics G3  = Graphics.FromHdc(G2.GetHdc());
      G3.DrawLine(new Pen(Color.Black, 10)851085100);
      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();
    }

  }
}



           
       
Related examples in the same category
1.Graphics PropertiesGraphics Properties
2.Simplest code to draw a lineSimplest code to draw a line
3.Draw radiation linesDraw radiation lines
4.illustrates the use of Pens and Linesillustrates the use of Pens and Lines
5.Draw line 1Draw line 1
6.Draw line 2Draw line 2
7.Line styleLine style
8.Draw line 3Draw line 3
9.Simplest Coordinate Simplest Coordinate
10.Draw a line 2Draw a line 2
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.