how to draw a square dot on mouse click on image picturebox c#

how to draw a square dot on mouse click on image picturebox c#

HI,
i need help on how to draw a square dot when fire mouse click:
-onto image in picturebox
-and return the position of dots (x,y) values.

here i try some code but missing in somewhere, please help me,

thanks

Regards
Aznimah

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RedDotAnnotate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Bitmap OriginalImage;
        Point myPts = Point.Empty;

        private void button1_Click(object sender, EventArgs e)
        {
            //Open File Dialog to load image files
            openFileDialog1.FileName = "";
            openFileDialog1.Title = "Images";

            //Filter the filedialog, so that it will show only the mentioned format images
            openFileDialog1.Filter = "PNG Image(*.png)|*.png|JPG Image(*.jpg)|*.jpg|BMP Image(*.bmp)|*.bmp";
            openFileDialog1.ShowDialog();

            if (openFileDialog1.FileName.ToString() != "")
            {
                pictureBox1.ImageLocation = openFileDialog1.FileName.ToString();
                OriginalImage = new Bitmap(openFileDialog1.FileName.ToString());
            }

            // butSave.Enabled = true;
        }

        private void pictureBox1_mouseDown(object sender, MouseEventArgs e)
        {
            Point myPts = new Point(e.X, e.Y);
            Bitmap b = (Bitmap)pictureBox1.Image;
            Graphics g = Graphics.FromImage(b);
           // myPts.add(new Point(e.X, e.Y));
            Invalidate();
            pictureBox1.Image = b;

        }
       // List<Point> myPts = new List<Point>();

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
           
            Graphics g = e.Graphics;
            foreach (Point p in myPts)
            {
                g.DrawEllipse(new Pen(Color.Green), p.X, p.Y, 10, 10);

            }            

        }
   
    }

  }

The Reddest
08/17/2010 - 09:42

What doesn't work in your code?

reply

aznimah
08/17/2010 - 21:48

Hi,

i try to code and run but whenever i try mouse click on image, the green dot(square)does not appear, any suggestion?

Thanks

reply

The Reddest
08/18/2010 - 09:12

Here's a solution. I subclassed PictureBox so I could override OnPaint. I tested this and it works pretty well.

public class PictureBoxEx : PictureBox
{
   List<Point> _points;

   public PictureBoxEx()
   {
      _points = new List<Point>();
      MouseDown += PictureBoxEx_MouseDown;
   }

   void PictureBoxEx_MouseDown(object sender, MouseEventArgs e)
   {
      _points.Add(new Point(e.X, e.Y));

      // Invalidate the control to cause it to repaint itself.
      this.Invalidate();
   }

   protected override void OnPaint(PaintEventArgs pe)
   {
      base.OnPaint(pe);

      foreach (var p in _points)
      {
         // Subtract 5 from the coords to center
         // the ellipse on the cursor.
         pe.Graphics.DrawEllipse(Pens.Green,
           new Rectangle(p.X - 5, p.Y - 5, 10, 10));
      }
   }
}

Simple add this control to your Form like you would any other PictureBox.

reply

Anonymous
08/29/2011 - 06:49

hiiii

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.