Click to See Complete Forum and Search --> : how to mark a cross on the picture in the picture box using c#


tty1358
July 20th, 2009, 12:44 AM
i wanted to mark a cross on the image in the picture box once i click the mouse there will be a marking like cross or dot , as i wanted to track back the time and signal at that point , i do not want to just draw . ty

rliq
July 20th, 2009, 01:09 AM
You can handle the PictureBox's MouseDown and/or MouseUp events. The MouseEventArgs parameter holds the information you need.

Not sure what you mean by time and signal? DateTime.Now ?

PictureBox.Invalidate() will cause it to be redrawn. Do all of your drawing in the PictureBox's Paint handler.

rliq
July 20th, 2009, 01:20 AM
This draw a circle, but you get the idea....

public partial class Form1 : Form
{
List<Point> _points = new List<Point>();

public Form1()
{
InitializeComponent();
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
PictureBox pb = sender as PictureBox;

_points.Add(e.Location);
pb.Invalidate(); // can be optimised by passing a Rectangle the same size as that used in the Paint handler
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;

foreach (Point p in _points)
{
g.DrawEllipse(Pens.Black, new Rectangle(p.X - 5, p.Y - 5, 10, 10));
}
}
}

rliq
July 20th, 2009, 01:26 AM
For a cross (as in a plus sign) replace the Paint handler with this:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;

foreach (Point p in _points)
{
Point pt1 = new Point(p.X, p.Y - 10);
Point pt2 = new Point(p.X, p.Y + 10);
Point pt3 = new Point(p.X - 10, p.Y);
Point pt4 = new Point(p.X + 10, p.Y);
g.DrawLine(Pens.Black, pt1, pt2);
g.DrawLine(Pens.Black, pt3, pt4);
}
}

rliq
July 20th, 2009, 01:45 AM
You can add a Label control to display the time of the click as follows:

List<Point> _points = new List<Point>();

public Form1()
{
InitializeComponent();
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
PictureBox pb = sender as PictureBox;

_points.Add(e.Location);
pb.Invalidate();

Label lbl = new Label();
pb.Controls.Add(lbl);

lbl.Location = new Point(e.Location.X + 4, e.Location.Y + 4);
lbl.Text = DateTime.Now.ToLongTimeString();

}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;

Point ptCurrent = new Point(0,0);

foreach (Point p in _points)
{
Point pt1 = new Point(p.X, p.Y - 10);
Point pt2 = new Point(p.X, p.Y + 10);
Point pt3 = new Point(p.X - 10, p.Y);
Point pt4 = new Point(p.X + 10, p.Y);
g.DrawLine(Pens.Black, pt1, pt2);
g.DrawLine(Pens.Black, pt3, pt4);

}
}