Click here to Skip to main content
15,881,882 members
Articles / Multimedia / GDI+
Tip/Trick

Simple Paint Application in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
25 Aug 2014CPOL 132.8K   14.6K   14   19
In this Tip/Trick, I am going to show How to create Simple Paint Application in C#

In this Tip and Trick, I am going to show you How to create Mini Paint Application using C# Windows Form Application . I am going to show some basic features of Paint Application. Before starting, I Hope you have the Basic Knowledge of GDI+ Graphics Functionality. I have added comments in the Code so that you can easily understand the code.

Let's understand the Mini Paint Application:

image1

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;
        //nullable int for storing Null value
        int? initX = null;
        int? initY = null;
        bool drawSquare = false;
        bool drawRectangle = false;
        bool drawCircle = false;
        //Event fired when the mouse pointer is moved over the Panel(pnl_Draw).
        private void pnl_Draw_MouseMove(object sender, MouseEventArgs e)
        {
            if(startPaint)
            {
                //Setting the Pen BackColor and line Width
                Pen p = new Pen(btn_PenColor.BackColor,float.Parse(cmb_PenSize.Text));
                //Drawing the line.
                g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y));
                initX = e.X;
                initY = e.Y;
            }
        }
        //Event Fired when the mouse pointer is over Panel and a mouse button is pressed
        private void pnl_Draw_MouseDown(object sender, MouseEventArgs e)
        {
            startPaint = true;
            if (drawSquare)
            {
                //Use Solid Brush for filling the graphic shapes
                SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
                //setting the width and height same for creating square.
                //Getting the width and Heigt value from Textbox(txt_ShapeSize)
                g.FillRectangle(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
                //setting startPaint and drawSquare value to false for creating one graphic on one click.
                startPaint = false;
                drawSquare = false;
            }
            if(drawRectangle)
            {
                SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
                //setting the width twice of the height
                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;
            }
        }
        //Fired when the mouse pointer is over the pnl_Draw and a mouse button is released.
        private void pnl_Draw_MouseUp(object sender, MouseEventArgs e)
        {
            startPaint = false;
            initX = null;
            initY = null;
        }
        //Button for Setting pen Color
        private void button1_Click(object sender, EventArgs e)
        {
            //Open Color Dialog and Set BackColor of btn_PenColor if user click on OK
            ColorDialog c = new ColorDialog();
            if(c.ShowDialog()==DialogResult.OK)
            {
                btn_PenColor.BackColor = c.Color;
            }
        }
        //New 
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Clearing the graphics from the Panel(pnl_Draw)
            g.Clear(pnl_Draw.BackColor);
            //Setting the BackColor of pnl_draw and btn_CanvasColor to White on Clicking New under File Menu
            pnl_Draw.BackColor = Color.White;
            btn_CanvasColor.BackColor = Color.White;
        }
       //Setting the Canvas Color
        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;
        }
        //Exit under File Menu
        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();
            }
        }
        //About under Help Menu
        private void aboutMiniPaintToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About a = new About();
            a.ShowDialog();
        }
    }
}

Final Preview:

image2

image3

You can also Download the Source File. Hope you like it.

Thanks.

My Other Posts (Tips and Tricks)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
India India

Comments and Discussions

 
QuestionDrag shapes, draw dynamic text in paint Pin
Aafaan_Jii15-Jun-22 19:25
Aafaan_Jii15-Jun-22 19:25 
QuestionHow do I load this code in 2019 VSCE without errors? Am I missing any GDI Libraries? Pin
Member 136760079-Oct-19 3:23
Member 136760079-Oct-19 3:23 
AnswerRe: How do I load this code in 2019 VSCE without errors? Am I missing any GDI Libraries? Pin
The Magical Magikarp2-Dec-19 7:49
The Magical Magikarp2-Dec-19 7:49 
QuestionThanks, man this helps a lot. Pin
Member 1442947322-May-19 18:33
Member 1442947322-May-19 18:33 
QuestionThanks Pin
Member 1406368622-Nov-18 3:12
Member 1406368622-Nov-18 3:12 
Questiongreat for beginers Pin
droid1530-Aug-17 14:04
droid1530-Aug-17 14:04 
AnswerRe: great for beginers Pin
Anoop Kr Sharma1-Sep-17 8:01
professionalAnoop Kr Sharma1-Sep-17 8:01 
Questionplace a scrollbar in your app Pin
Member 284105229-May-17 23:03
Member 284105229-May-17 23:03 
Questionsaving the picture ??? Pin
MuhammedSaneef22-Feb-16 21:20
MuhammedSaneef22-Feb-16 21:20 
Buglosting data and no save button Pin
Member 1117996130-Jun-15 1:49
Member 1117996130-Jun-15 1:49 
Questionproject cannot be loaded. Pin
Member 1139767223-Jan-15 7:10
Member 1139767223-Jan-15 7:10 
GeneralRe: project cannot be loaded. Pin
Anoop Kr Sharma23-Jan-15 16:11
professionalAnoop Kr Sharma23-Jan-15 16:11 
GeneralRe: project cannot be loaded. Pin
The Magical Magikarp2-Dec-19 7:50
The Magical Magikarp2-Dec-19 7:50 
QuestionThanks Pin
Frans_5512928-Aug-14 2:05
Frans_5512928-Aug-14 2:05 
GeneralRe: Thanks Pin
Anoop Kr Sharma4-Sep-14 6:18
professionalAnoop Kr Sharma4-Sep-14 6:18 
QuestionCode block Pin
Nelek25-Aug-14 23:12
protectorNelek25-Aug-14 23:12 
GeneralRe: Code block Pin
Anoop Kr Sharma26-Aug-14 3:23
professionalAnoop Kr Sharma26-Aug-14 3:23 
GeneralRe: Code block Pin
Nelek26-Aug-14 5:22
protectorNelek26-Aug-14 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.