how to mark a cross on the picture in the picture box using c#
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
Re: how to mark a cross on the picture in the picture box using c#
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.
Re: how to mark a cross on the picture in the picture box using c#
This draw a circle, but you get the idea....
Code:
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));
}
}
}
Re: how to mark a cross on the picture in the picture box using c#
For a cross (as in a plus sign) replace the Paint handler with this:
Code:
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);
}
}
Re: how to mark a cross on the picture in the picture box using c#
You can add a Label control to display the time of the click as follows:
Code:
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);
}
}