C# tutorial-C# drawing line with pen


C# drawing line with pen

After the graphic object is created by using CreateGraphics() method of the control. To draw a line on the control, you can use the drawLine() method of the graphic object. The drawLine() method requires three parameters. The first parameter is a pen object. You can specify the color of line to draw with the pen object. The other two parameters are point objects--start and end points of the line.



Example: drawing two different color lines on the form

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace WindowsFormsApplication2

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

 

 

        private void Form2_Paint(object sender, PaintEventArgs e)

        {

           

            //Create graphic object for the current form

            Graphics gs = this.CreateGraphics();

            //Create brush object

            Brush br1 = new SolidBrush(Color.Red );

            Brush br2 = new SolidBrush(Color.CadetBlue);

            //Create pen objects

            Pen p1 = new Pen(br1);

            Pen p2 = new Pen(br2);

            //Draw lines

            gs.DrawLine(p1, new Point(20, 100), new Point(200, 100));

            gs.DrawLine(p2, new Point(120, 10), new Point(120, 200));

        }

    }

C# to draw lines  

 




Comments

CAPTCHA image




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.