Drawing with Mouse in C# 5.0



This C# Windows Forms tutorial is a bit advanced program which shows a glimpse of  how we can use the graphics object in C#. Our aim is to create a form and paint over it with the help of the mouse just as a 'Pencil'. Very similar to the 'Pencil' in MS Paint. We start off by creating a graphics object which is the form in this case. A graphics object provides us a surface area to draw to. We draw small ellipses which appear as dots. These dots are produced frequently as long as the left mouse button is pressed. When the mouse is dragged, the dots are drawn over the path of the mouse. This is achieved with the help of the MouseMove event which means the dragging of the mouse. We simply write code to draw ellipses each time a MouseMove event is fired. 


Also on right clicking the Form, the background color is changed to a random color. This is achieved by picking a random value for Red, Blue and Green through the random class. The Random object gives us a random integer value to feed the Red, Blue & Green values of Color.FromArgb() function(Which are between 0 and 255 for a 32 bit color interface). The argument e gives us the source for identifying the button pressed which in this case is desired to be MouseButtons.Right i.e. the Right Mouse Button. The Graphics Surface used to draw the Ellipse is the Form itself which is referenced by the this keyword. The CreateGraphics() Method is used to transform any Control on the Form or the Form itself to a drawing surface and return it to the Graphics() Object on the left of the assignment operator. We have drawn small ellipses on the Form of size 5 pixels in Width and Height using the Pen named p. Whenever we have pressed a Color button, the COlor property of the Pen p is changed to a required color. This Pen is then passed as the parameter when drawing the Ellipses. 


using System;
using System.Drawing;
using System.Windows.Forms;

namespace Mouse_Paint
{
    public partial class MainForm : Form
    {
              
        public MainForm()
        {
            InitializeComponent();
        }

        private Graphics m_objGraphics;
        Random rd = new Random();
        Pen p = new Pen(Color.Black);
       
        private void MainForm_Load(object sender, EventArgs e)
        {
            m_objGraphics = this.CreateGraphics();
        }
        private void MainForm_Close(object sender, EventArgs e)
        {
            m_objGraphics.Dispose();
        }
        private void MainForm_Click(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                m_objGraphics.Clear(Color.FromArgb(rd.Next(0,255),rd.Next(0,255),rd.Next(0,255)));
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            Rectangle rectEllipse = new Rectangle();
            if (e.Button != MouseButtons.Left) return;
            rectEllipse.X = e.X - 1;
            rectEllipse.Y = e.Y - 1;
            rectEllipse.Width = 5;
            rectEllipse.Height = 5;
            m_objGraphics.DrawEllipse(p, rectEllipse);
           
        }

        private void btnPurple_Click(object sender, EventArgs e)
        {
            p.Color = Color.Purple;
        }

        private void btnBlue_Click(object sender, EventArgs e)
        {
            p.Color = Color.Blue;
        }

        private void btnTurquoise_Click(object sender, EventArgs e)
        {
            p.Color = Color.Turquoise;
        }

        private void btnGreen_Click(object sender, EventArgs e)
        {
            p.Color = Color.Green;
        }

        private void btnYellow_Click(object sender, EventArgs e)
        {
            p.Color = Color.Yellow;
        }

        private void btnOrange_Click(object sender, EventArgs e)
        {
            p.Color = Color.Orange;
        }
    }
}




Please Note :
** Do not Copy & Paste code written here ; instead type it in your Development Environment
** Testing done in .Net Framework 4.5 but code should be very similar for previous versions of .Net
** All Program Codes here are 100%  tested & running.