C# tutorial-C# drawing rectangles


C# drawing rectangles

To draw a rectangle shape on the control, you can use the drawRectangle() method of the graphic object. The drawRectangle() method requires two parameters. The first parameter is a pen object. The second is the rectangle object. If you want to draw many rectangles, the drawRectangles() method can be used instead. By using the latter method, you can pass an array of rectangle objects to draw.

Example: drawing three rectangles on the form using two different methods

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 rectangle objects

            Rectangle rt1 = new Rectangle(30,90,100,200);

            Rectangle[] Crect = { new Rectangle(140, 90, 200, 200), new Rectangle(350, 90, 400, 200) }; 

            //Create pen objects

            Pen p = new Pen(new SolidBrush(Color.Green));

            Pen p1 = new Pen(new SolidBrush(Color.BlueViolet));

            //Draw rectangles

            gs.DrawRectangle(p, rt1); //draw one rectangle

            gs.DrawRectangles(p1, Crect); //draw more two rectangles

 

        }

    }

}

C# to drawing rectangle




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.