Mini Paint Application Using C#

This article shows how to create a Mini Paint Application using a C# Windows Forms application. I will show some basic features of the paint application. Before starting, I hope you have a basic undersanding of GDI+ Graphics. I have added comments to the code so that you can easily understand the code.
 
Let's understand the Mini Paint Application structure:
 
 
 
Here is the code used in this application:
  1. using System;  
  2. using System.Drawing;  
  3. using System.Windows.Forms;  
  4.   
  5. namespace MiniPaint  
  6. {  
  7.     public partial class Form1 : Form  
  8.     {  
  9.         public Form1()  
  10.         {  
  11.             InitializeComponent();  
  12.             g = pnl_Draw.CreateGraphics();  
  13.         }  
  14.         bool startPaint = false;  
  15.         Graphics g;  
  16.         //nullable int for storing Null value  
  17.         int? initX = null;  
  18.         int? initY = null;  
  19.         bool drawSquare = false;  
  20.         bool drawRectangle = false;  
  21.         bool drawCircle = false;  
  22.         //Event fired when the mouse pointer is moved over the Panel(pnl_Draw).  
  23.         private void pnl_Draw_MouseMove(object sender, MouseEventArgs e)  
  24.         {  
  25.             if(startPaint)  
  26.             {  
  27.                 //Setting the Pen BackColor and line Width  
  28.                 Pen p = new Pen(btn_PenColor.BackColor,float.Parse(cmb_PenSize.Text));  
  29.                 //Drawing the line.  
  30.                 g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y));  
  31.                 initX = e.X;  
  32.                 initY = e.Y;  
  33.             }  
  34.         }  
  35.         //Event Fired when the mouse pointer is over Panel and a mouse button is pressed  
  36.         private void pnl_Draw_MouseDown(object sender, MouseEventArgs e)  
  37.         {  
  38.             startPaint = true;  
  39.             if (drawSquare)  
  40.             {  
  41.                 //Use Solid Brush for filling the graphic shapes  
  42.                 SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);  
  43.                 //setting the width and height same for creating square.  
  44.                 //Getting the width and Heigt value from Textbox(txt_ShapeSize)  
  45.                 g.FillRectangle(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));  
  46.                 //setting startPaint and drawSquare value to false for creating one graphic on one click.  
  47.                 startPaint = false;  
  48.                 drawSquare = false;  
  49.             }  
  50.             if(drawRectangle)  
  51.             {  
  52.                 SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);  
  53.                 //setting the width twice of the height  
  54.                 g.FillRectangle(sb, e.X, e.Y, 2*int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));  
  55.                 startPaint = false;  
  56.                 drawRectangle = false;  
  57.             }  
  58.             if(drawCircle)  
  59.             {  
  60.                 SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);  
  61.                 g.FillEllipse(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));  
  62.                 startPaint = false;  
  63.                 drawCircle = false;  
  64.             }  
  65.         }  
  66.         //Fired when the mouse pointer is over the pnl_Draw and a mouse button is released.  
  67.         private void pnl_Draw_MouseUp(object sender, MouseEventArgs e)  
  68.         {  
  69.             startPaint = false;  
  70.             initX = null;  
  71.             initY = null;  
  72.         }  
  73.         //Button for Setting pen Color  
  74.         private void button1_Click(object sender, EventArgs e)  
  75.         {  
  76.             //Open Color Dialog and Set BackColor of btn_PenColor if user click on OK  
  77.             ColorDialog c = new ColorDialog();  
  78.             if(c.ShowDialog()==DialogResult.OK)  
  79.             {  
  80.                 btn_PenColor.BackColor = c.Color;  
  81.             }  
  82.         }  
  83.         //New   
  84.         private void newToolStripMenuItem_Click(object sender, EventArgs e)  
  85.         {  
  86.             //Clearing the graphics from the Panel(pnl_Draw)  
  87.             g.Clear(pnl_Draw.BackColor);  
  88.             //Setting the BackColor of pnl_draw and btn_CanvasColor to White on Clicking New under File Menu  
  89.             pnl_Draw.BackColor = Color.White;  
  90.             btn_CanvasColor.BackColor = Color.White;  
  91.         }  
  92.        //Setting the Canvas Color  
  93.         private void btn_CanvasColor_Click_1(object sender, EventArgs e)  
  94.         {  
  95.             ColorDialog c = new ColorDialog();  
  96.             if(c.ShowDialog()==DialogResult.OK)  
  97.             {  
  98.                 pnl_Draw.BackColor = c.Color;  
  99.                 btn_CanvasColor.BackColor = c.Color;  
  100.             }  
  101.         }  
  102.   
  103.         private void btn_Square_Click(object sender, EventArgs e)  
  104.         {  
  105.             drawSquare = true;  
  106.         }  
  107.   
  108.         private void btn_Rectangle_Click(object sender, EventArgs e)  
  109.         {  
  110.             drawRectangle = true;  
  111.         }  
  112.   
  113.         private void btn_Circle_Click(object sender, EventArgs e)  
  114.         {  
  115.             drawCircle = true;  
  116.         }  
  117.         //Exit under File Menu  
  118.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)  
  119.         {  
  120.             if(MessageBox.Show("Do you want to Exit?","Exit",MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes)  
  121.             {  
  122.                 Application.Exit();  
  123.             }  
  124.         }  
  125.         //About under Help Menu  
  126.         private void aboutMiniPaintToolStripMenuItem_Click(object sender, EventArgs e)  
  127.         {  
  128.             About a = new About();  
  129.             a.ShowDialog();  
  130.         }  
  131.   
  132.     }  
  133. }  
Final preview:
 
 
 
 I hope you like it. Thanks.