C# tips : How to draw on a picturebox image using mouse by c#

if u want to draw something on a picture u can use microsoft paint, but if you want do it by ur c# code, i think this article will help you.

i make a win form application to draw by mouse. u can select the pensize as u wish. It is only a tricks between three mouse event on a picture they are

  • mouse up
  • mouse down
  • mouse move

to use the software to u have to insert an image to picture box then chose color and size of brush. then start to draw by mouse press on image. like below image

after inserting u can draw as u like

the coding is fairly easy, u have to check mouse event is pressed or not, the write a rectangle in mouse position. like below

 bool draw = false;
   int s = 3;
  Color color = Color.Red;
 private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            draw = true;
            Graphics g = Graphics.FromImage(mainimage);
            Pen pen1 = new Pen(color, 4);
            g.DrawRectangle(pen1, e.X, e.Y, 2, 2);
            g.Save();
            pictureBox1.Image = mainimage;
        }
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            draw = false;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (draw)
            {
                Graphics g = Graphics.FromImage(mainimage);
                SolidBrush brush = new SolidBrush(color);
                g.FillRectangle(brush, e.X, e.Y, s, s);
                g.Save();
                pictureBox1.Image = mainimage;
            }
        }

u can get full source code from here

Advertisements

About kishordgupta

A software developer
This entry was posted in C#, Image Processing, Winform app and tagged , , , , , , , . Bookmark the permalink.

13 Responses to C# tips : How to draw on a picturebox image using mouse by c#

  1. Pingback: C# tips : How to draw on a picturebox image using mouse by c# … | Learn Visual Studio

  2. Anonymous says:

    this code help me very much to complete my project
    thank u

  3. Vyshak says:

    Hi,Your Blog is great it features some the best in depth coding articles.I have a coding problem can you please help me out.

    I’m trying to implement a image editor,i want to draw an image on top of existing image on a picture box.I have achieved it using Graphics.DrawImage and using the MouseEventArguments point.But the problem is i want a selection rectangle drawn on top of the image being drawn when the mouse is clicked over it and it should be redrawn at a new position obtained by on click and drag by the mouse(without selection rectangle).The rending of the image at the new position is slow and lots of memory is consumed so i use GC.collect.Please help me to implement the selection.

  4. Anonymous says:

    thank you kishore

  5. Mary says:

    Can I ask why the drawing point isn’t the exact mouse pointer location? On mine, it draws a couple of pixels ‘off’… Any help would be appreciated 🙂 Thanks

  6. anonymous2 says:

    Hi, is there another location from the code? I couldn´t download it from the source you post.

    Thanx

  7. Tommy says:

    Yes, is there another location for this code? I couldn’t download it either and it looks like it would be very helpful. Thanks.

  8. Anonymous says:

    Hi, I couldn’t download it either. Is there another location for the code?

  9. Anonymous says:

    thanx a lot ! this wz too easy..but wont hv been possible w/o your guidance… 🙂

  10. Anonymous says:

    thanks. But what is mainimage ?

  11. Anonymous says:

    c quoi le mainimage

  12. Nikè says:

    Thanks for that code, it helps me a lot.
    Now i’ve written a code which make my camera view visible into one picturebox. I want to know how to do the same on that video (draw a line, a rectangle or things like that). Thank you

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s