Saturday, February 11, 2012

Drawing a basic shape in windows form application using C#

There consists of the set of .NET base classes that are available to control custom drawing on the screen.Here we learn how to draw basic shapes in windows form using C#. This articles is simple collection which I read while doing graphics projects.
Before starting create a new project in windows form application using C# and I assume that your name of form is Form1. Increase the size of form up to 720*600 in properties windows.Now we draw a shapes by creating graphics object and pen or brush object and these class are from namespace system.Drawing.
At first we have to initialize the paint event handler by adding
this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint); 

line of code in Form1.Designer.cs inside private void InitializeComponent() method. If you don’t do this  then your image won’t draw in form although compiler doesn’t shows any error.



To draw basics shapes you must follow these steps:
  1. Create a Graphics object by calling System.Windows.Forms.Control.CreateGraphics method.
  2. Create a Pen object
  3. Call a member of the Graphics class to draw on the control using the Pen
for example

 
//creating a graphics object from the form
   Graphics g = this.CreateGraphics();
// Creating pen and brush object
   Pen blackPen = new Pen(Color.Black);         
   SolidBrush drawBrushObj = new SolidBrush(Color.Blue);
// drawing the line by
// calling a member of the graphics class and pen object
   g.DrawLine(p, 5, 40,100 , 40);
            

You can learn all the properties and methods of namespace system.Drawing form msdn site
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx

Here is the complete project source code

Form1.cs


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 DrawingBasicShapes
{
    public partial class Form1 : System.Windows.Forms.Form
    {
       // public Bitmap myBitmap = new Bitmap(1,1);
        public Form1()
        {
            InitializeComponent();
            CenterToScreen();
            SetStyle(ControlStyles.ResizeRedraw, true); 
        }
        private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            // Create string to draw.
            String drawStringline = "Draw line";
            String drawStringellipse = "Draw Ellipse";
            String drawStringFillellipse = "Draw Fill Ellipse";
            String drawStringcurve = "Draw closed curve";
            //String drawStringellipse = "Draw ellipse";
            //String drawStringellipse = "Draw ellipse";
            //String drawStringellipse = "Draw ellipse";
            //String drawStringellipse = "Draw ellipse";
 
 
            //create a graphics object from the form
            Graphics g = this.CreateGraphics();
            // Create font and brush.
            Pen blackPen = new Pen(Color.Black);
            Font drawFont = new Font("Arial", 16);
            SolidBrush drawBrush = new SolidBrush(Color.Black);
            SolidBrush drawBrushObj = new SolidBrush(Color.Blue);
            Pen p = new Pen(Color.Red, 2);
 
            //*----------Drawing line-------------*
 
            
            RectangleF drawRect1 = new RectangleF(5.0f, 5.0f, 100.0f, 25.0f);
 
            // Draw rectangle to screen.
            g.DrawRectangle(blackPen, 5.0f, 5.0f, 100.0f, 25.0f);
 
            // Draw string to screen.
            g.DrawString(drawStringline, drawFont, drawBrush, drawRect1);
            // create  a  pen object with which to draw
              
            // draw the line 
            // call a member of the graphics class
            g.DrawLine(p, 5, 40,100 , 40);
 
 
 
            //*----------Draw ellipse-------------*
            RectangleF drawRect2 = new RectangleF(115.0f, 5.0f, 150.0f, 25.0f);
 
            // Draw rectangle to screen.
            g.DrawRectangle(blackPen, 115.0f, 5.0f, 150.0f, 25.0f);
 
            // Draw string to screen.
            g.DrawString(drawStringellipse, drawFont, drawBrush, drawRect2);
            // create  a  pen object with which to draw
 
 
            Rectangle rect = new Rectangle(150, 35, 100, 50);
            // Draw ellipse to screen.
            g.DrawEllipse(p, rect);
 
 
            //*----------Draw Fill ellipse-------------*
            RectangleF drawRect3 = new RectangleF(275.0f, 5.0f, 175.0f, 25.0f);
            // Draw rectangle to screen.
            g.DrawRectangle(blackPen, 275.0f, 5.0f, 175.0f, 25.0f);
            // Draw string to screen.
            g.DrawString(drawStringFillellipse, drawFont, drawBrush, drawRect3);
            // Fill ellipse on screen.
            g.FillEllipse(drawBrushObj, 275, 35, 150, 50);
 
 
            //*---------Draw closed Curve--------------*
            RectangleF drawRect4 = new RectangleF(460.0f, 5.0f, 200.0f, 25.0f);
            // Draw rectangle to screen.
            g.DrawRectangle(blackPen, 460.0f, 5.0f, 200.0f, 25.0f);
            // Draw string to screen.
            g.DrawString(drawStringcurve, drawFont, drawBrush, drawRect4);
 
            // Create points that define curve.
            Point point1 = new Point(470, 45);
            Point point2 = new Point(500, 85);
            Point point3 = new Point(530, 125);
            Point point4 = new Point(560, 150);
            Point point5 = new Point(590, 125);
            Point point6 = new Point(620, 100);
            Point point7 = new Point(650, 45);
            Point[] curvePoints = { point1, point2, point3, point4, point5, point6, point7 };
 
            // Draw closed curve to screen.
            g.DrawClosedCurve(p, curvePoints);
 
           
        }
 
 
    }
}




Form1.Designer.cs




namespace DrawingBasicShapes
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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.SuspendLayout();
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(720, 342);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);
            this.ResumeLayout(false);
 
        }
 
        #endregion
    }
}
 






Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
 
namespace DrawingBasicShapes
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}




Screenshot of Output

Basic_shapes

No comments:

Post a Comment