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:
- using System;
- using System.Drawing;
- using System.Windows.Forms;
-
- namespace MiniPaint
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- g = pnl_Draw.CreateGraphics();
- }
- bool startPaint = false;
- Graphics g;
-
- int? initX = null;
- int? initY = null;
- bool drawSquare = false;
- bool drawRectangle = false;
- bool drawCircle = false;
-
- private void pnl_Draw_MouseMove(object sender, MouseEventArgs e)
- {
- if(startPaint)
- {
-
- Pen p = new Pen(btn_PenColor.BackColor,float.Parse(cmb_PenSize.Text));
-
- g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y));
- initX = e.X;
- initY = e.Y;
- }
- }
-
- private void pnl_Draw_MouseDown(object sender, MouseEventArgs e)
- {
- startPaint = true;
- if (drawSquare)
- {
-
- SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
-
-
- g.FillRectangle(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
-
- startPaint = false;
- drawSquare = false;
- }
- if(drawRectangle)
- {
- SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
-
- g.FillRectangle(sb, e.X, e.Y, 2*int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
- startPaint = false;
- drawRectangle = false;
- }
- if(drawCircle)
- {
- SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
- g.FillEllipse(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
- startPaint = false;
- drawCircle = false;
- }
- }
-
- private void pnl_Draw_MouseUp(object sender, MouseEventArgs e)
- {
- startPaint = false;
- initX = null;
- initY = null;
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
-
- ColorDialog c = new ColorDialog();
- if(c.ShowDialog()==DialogResult.OK)
- {
- btn_PenColor.BackColor = c.Color;
- }
- }
-
- private void newToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- g.Clear(pnl_Draw.BackColor);
-
- pnl_Draw.BackColor = Color.White;
- btn_CanvasColor.BackColor = Color.White;
- }
-
- private void btn_CanvasColor_Click_1(object sender, EventArgs e)
- {
- ColorDialog c = new ColorDialog();
- if(c.ShowDialog()==DialogResult.OK)
- {
- pnl_Draw.BackColor = c.Color;
- btn_CanvasColor.BackColor = c.Color;
- }
- }
-
- private void btn_Square_Click(object sender, EventArgs e)
- {
- drawSquare = true;
- }
-
- private void btn_Rectangle_Click(object sender, EventArgs e)
- {
- drawRectangle = true;
- }
-
- private void btn_Circle_Click(object sender, EventArgs e)
- {
- drawCircle = true;
- }
-
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if(MessageBox.Show("Do you want to Exit?","Exit",MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes)
- {
- Application.Exit();
- }
- }
-
- private void aboutMiniPaintToolStripMenuItem_Click(object sender, EventArgs e)
- {
- About a = new About();
- a.ShowDialog();
- }
-
- }
- }
Final preview:
I hope you like it. Thanks.