Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#

Paint Brush

Rate me:
Please Sign up or sign in to vote.
3.77/5 (10 votes)
15 Dec 2008CPOL2 min read 80.7K   20   13
Paint Brush tool is similar to the pencil (Free hand) with more features. It can be used in different colors, the shape and size of the Paint Brush can also be changed.
1.gif

Introduction

Paint Brush tool is similar to the pencil (free hand) with more features. It can be used in different colors, the shape and size of the Paint Brush can also be changed.

Project Idea

2.gif

Global Variable

C#
private int x1, y1, x2, y2;
      private bool flag = false;
      private bool colorFlag = false;
      private Label lbl;
      private ArrayList sequenceStore = new ArrayList();

Steps

  1. In mouse down:
    C#
    private void panel1_MouseDown(object sender, MouseEventArgs e)
          {
              try
              {
                  x1 = e.X;
                  y1 = e.Y;
               }
              catch (Exception)
              {
                  throw;
              }
          }
    
  2. In mouse move:
    C#
    x2 = e.X;
    y2 = e.Y;
    Graphics g = Graphics.FromHwnd(panel1.Handle);
    
    //default pen
    Pen p = new Pen(Color.Red, (float)comboBox1.SelectedIndex);
    if (colorFlag == true)
    {
        p = new Pen(lbl.BackColor, (float)comboBox1.SelectedIndex);
    }
    Point p1 = new Point(x1, y1);
    Point p2 = new Point(x2, y2);
    g.DrawLine(p, p1, p2);
    LineStoreGraph linestrgrph = new LineStoreGraph(p, x1, y1, x2, y2);
    sequenceStore.Add(linestrgrph);
    x1 = x2;
    y1 = y2;

Problem

If we run the previous code, the free hand will work but if we stop while drawing, we will note that pen is still drawing the mouse up. So we will make a flag and test through it where flag will be true if mouse is down and set flag to be false while mouse is up.

C#
private void panel1_MouseDown(object sender, MouseEventArgs e)
     {
         try
         {
             x1 = e.X;
             y1 = e.Y;
             flag = true;
         }
         catch (Exception)
         {
             throw;
         }
     }
C#
private void panel1_MouseMove(object sender, MouseEventArgs e)
       {
           try
           {
               if (flag == true)
               {
                   x2 = e.X;
                   y2 = e.Y;
                   Graphics g = Graphics.FromHwnd(panel1.Handle);

                   //default pen
                   Pen p = new Pen(Color.Red, (float)comboBox1.SelectedIndex);
                   if (colorFlag == true)
                   {
                       p = new Pen(lbl.BackColor, (float)comboBox1.SelectedIndex);
                   }
                   Point p1 = new Point(x1, y1);
                   Point p2 = new Point(x2, y2);
                   g.DrawLine(p, p1, p2);
                   LineStoreGraph linestrgrph = new LineStoreGraph(p, x1, y1, x2, y2);
                   sequenceStore.Add(linestrgrph);
                   x1 = x2;
                   y1 = y2;

               }
           }
           catch (Exception)
           {
               throw;
           }
       }
C#
private void panel1_MouseMove(object sender, MouseEventArgs e)
       {
           try
           {
               if (flag == true)
               {
                   x2 = e.X;
                   y2 = e.Y;
                   Graphics g = Graphics.FromHwnd(panel1.Handle);

                   //default pen
                   Pen p = new Pen(Color.Red, (float)comboBox1.SelectedIndex);
                   if (colorFlag == true)
                   {
                       p = new Pen(lbl.BackColor, (float)comboBox1.SelectedIndex);
                   }
                   Point p1 = new Point(x1, y1);
                   Point p2 = new Point(x2, y2);
                   g.DrawLine(p, p1, p2);
                   LineStoreGraph linestrgrph = new LineStoreGraph(p, x1, y1, x2, y2);
                   sequenceStore.Add(linestrgrph);
                   x1 = x2;
                   y1 = y2;

               }
           }
           catch (Exception)
           {
               throw;
           }
       }

Change Color of Pencil

Steps

In event of Label: make all labels in one event:

C#
private void label1_Click(object sender, EventArgs e)
        {
            lbl = (Label)sender;
            colorFlag = true;
        }

Note: I add a flag named colorflag because I give a default color to pen when the app runs first. I depend on getting color if user presses click on label, so if I do not make this flag and user starts drawing without choosing color, an exception will occur. I avoid this by giving a default color and making something like a sensor to know if user clicks on label, and if this done I will switch to taking the user's color.

Change Size of Pencil

C#
public Form1()
        {
            InitializeComponent();

            for (int i = 6; i <= 30; i++)
            {
                comboBox1.Items.Add(i);
            }
            comboBox1.Text = comboBox1.Items[0].ToString();            
        }

And as we do in mouse Move, we change the constructor of pen to receive its value from combox1.

C#
Pen p = new Pen(Color.Red, (float)comboBox1.SelectedIndex);

Surprise!!!

After we ended from paint brush if we minimize the form to taskbar we will note that the form does not contain any graphics and is like a blank page.

Solution

We will store each point p1, p2 and pen and for each line we do, we will store it in Arraylist then we will retrieve data stored in Arraylist in event Paint.

C#
Point p1 = new Point(x1, y1);
                    Point p2 = new Point(x2, y2);
                    g.DrawLine(p, p1, p2);
                    LineStoreGraph linestrgrph = new LineStoreGraph(p, x1, y1, x2, y2);
                    sequenceStore.Add(linestrgrph);

And we will retrieve the values we store:

C#
private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = Graphics.FromHwnd(panel1.Handle);
            foreach (LineStoreGraph lineSG in sequenceStore)
            {
                g.DrawLine(lineSG.p, lineSG.x1, lineSG.y1, lineSG.x2, lineSG.y2);
            }
        }

Save Your Image:

C#
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
       {
           saveFileDialog1.Filter = "All supported image types|" +
             "*.bmp;*.jpg;*.jpeg;*.gif|Bitmaps (*.bmp)|*.bmp|JPEG Images" +
             " (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif";
           saveFileDialog1.FilterIndex = 1;
           Bitmap mybitmap = new Bitmap(panel1.Width, panel1.Height);
           for (int i = 0; i < mybitmap.Width; i++)
           {
               for (int j = 0; j < mybitmap.Height; j++)
               {
                   mybitmap.SetPixel(i, j, Color.White);
               }
           }
           if (saveFileDialog1.ShowDialog() == DialogResult.OK)
           {
               Graphics g = Graphics.FromImage(mybitmap);
               g.SmoothingMode =
                  System.Drawing.Drawing2D.SmoothingMode.HighQuality;
               LineStoreGraph lineSG;
               Pen p = new Pen(Color.Red, 1);
               p.StartCap = System.Drawing.Drawing2D.LineCap.Round;
               p.EndCap = System.Drawing.Drawing2D.LineCap.Round;
               for (int i = 0; i < sequenceStore.Count; i++)
               {
                   lineSG = (LineStoreGraph)sequenceStore[i];
                   g.DrawLine(lineSG.p, lineSG.x1, lineSG.y1,
                              lineSG.x2, lineSG.y2);
               }
               mybitmap.Save(saveFileDialog1.FileName);
           }
       }

Future Development

If Allah wishes, I will add the following:

  • Add undo & redo to lines
  • Open image
  • Make paint brush client-server

History

  • 15th December, 2008: Initial post 

License

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


Written By
Web Developer ITeShare
Egypt Egypt
I am interested in Software architecture, Requirements Engineering and coding. I am MCP ,SCJP,MCTs,MCPD, Brainbench.

http://ahmedbohoty.blogspot.com/

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey16-Feb-12 23:51
professionalManoj Kumar Choubey16-Feb-12 23:51 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey16-Feb-12 20:21
professionalManoj Kumar Choubey16-Feb-12 20:21 
QuestionCode is on 4shared.com Pin
mihi6418-Oct-11 13:45
mihi6418-Oct-11 13:45 
GeneralMy vote of 1 Pin
Alavijeh7-Aug-10 10:11
Alavijeh7-Aug-10 10:11 
GeneralMy vote of 5 Pin
Honeyboy_202-Jul-10 12:56
Honeyboy_202-Jul-10 12:56 
Generalpleease Pin
Raef Ezz15-Mar-10 7:27
Raef Ezz15-Mar-10 7:27 
Generali can't download the source code can u reply me soon plz i need it thnx very much Pin
Raef Ezz13-Mar-10 12:53
Raef Ezz13-Mar-10 12:53 
Generalsource code is not downloadable Pin
naorem10-Mar-10 22:45
naorem10-Mar-10 22:45 
GeneralThanks ... Pin
Honeyboy_201-Jan-09 13:55
Honeyboy_201-Jan-09 13:55 
GeneralMy vote of 1 Pin
Bishoy Demian27-Dec-08 21:38
Bishoy Demian27-Dec-08 21:38 
bad coding practices, bad exception handling, bad OO design
GeneralRe: My vote of 1 Pin
Ahmed R El Bohoty1-Jan-09 10:23
Ahmed R El Bohoty1-Jan-09 10:23 
GeneralRe: My vote of 1 Pin
Honeyboy_206-Jun-10 16:51
Honeyboy_206-Jun-10 16:51 
GeneralGood One Pin
Jamal Alqabandi24-Dec-08 22:38
Jamal Alqabandi24-Dec-08 22:38 

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.