How can i keep/save the drawing im doing on the current frame(image) ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
In my project i extract images from a video file. Then i can draw on the pictureBox1 in the pictureBox1_Paint event.
Now i want to make that if i draw anything on the current frame i see in the pictureBox1 it will save/keep what i have drawed.
And if i move to the next frame/image with the trackBar1 ill see only the image it self without the drawing i did on the frame/image before.
And if i go back in the trackBar1 to the first image ill see the drawing again. And if i draw anything on the second frame/image so to keep it too and so on.

This is the paint event with all the picturebox1 mouse events:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private <span style="color:Blue; bool LoadPictureAt(<span style="color:Blue; int nIndex, <span style="color:Blue; object c)
{

<span style="color:Blue; bool bRet = <span style="color:Blue; false;

<span style="color:Blue; if (nIndex >= 0 && nIndex < fi.Length)
{
<span style="color:Blue; if (c.Equals(trackBar1))

pictureBox1.Load(fi[nIndex].FullName);
bRet = <span style="color:Blue; true;

}

<span style="color:Blue; return bRet;
}

<span style="color:Blue; private <span style="color:Blue; void trackBar1_Scroll(<span style="color:Blue; object sender, EventArgs e)
{
LoadPictureAt(trackBar1.Value, sender);
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_Paint(<span style="color:Blue; object sender, PaintEventArgs e)
{
<span style="color:Blue; if (_points != <span style="color:Blue; null)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
<span style="color:Blue; using (SolidBrush myBrush = <span style="color:Blue; new SolidBrush(Color.Red))
{
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < _points.Count; i++)
{
<span style="color:Blue; try
{
Point pt = _points.Point;
e.Graphics.FillEllipse(myBrush, <span style="color:Blue; new RectangleF(pt.X - _circleWidth / 2F, pt.Y - _circleWidth / 2F, _circleWidth, _circleWidth));
<span style="color:Blue; if (i > 0)
{
<span style="color:Blue; if (_points.Connected)
e.Graphics.DrawLine(Pens.Blue, _points[i - 1].Point, _points.Point);
}
}
<span style="color:Blue; catch ( Exception err)
{
textBox1.Text = err.ToString();
}
}

}

}


}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_MouseDown(<span style="color:Blue; object sender, MouseEventArgs e)
{

<span style="color:Blue; if (e.Button == MouseButtons.Left)
{
<span style="color:Blue; if (_points == <span style="color:Blue; null)
_points = <span style="color:Blue; new List<LinePoint>();

_current = HitTest(e.X, e.Y);
<span style="color:Blue; if (_current != <span style="color:Blue; null)
{
_pt = <span style="color:Blue; new Point(e.X, e.Y);
}
<span style="color:Blue; else
{
_points.Add(<span style="color:Blue; new LinePoint() { Point = <span style="color:Blue; new Point(e.X, e.Y), Connected = _connect });

<span style="color:Blue; if (_connect && _points.Count > 1)
_points[_points.Count - 1].Connection = _points[_points.Count - 2];

}

pictureBox1.Invalidate();
}

<span style="color:Blue; if (e.Button == MouseButtons.Right)
{
_connect = !_connect;
_rightButton = <span style="color:Blue; true;
}

}

<span style="color:Blue; private LinePoint HitTest(<span style="color:Blue; int x, <span style="color:Blue; int y)
{
<span style="color:Blue; if (_points == <span style="color:Blue; null) <span style="color:Blue; return <span style="color:Blue; null;

<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < _points.Count; i++)
<span style="color:Blue; if (_points.HitTest(x, y, _circleWidth))
{
_points.IsCurrent = <span style="color:Blue; true;
<span style="color:Blue; return _points;
}

<span style="color:Blue; return <span style="color:Blue; null;
}



<span style="color:Blue; private <span style="color:Blue; void pictureBox1_DoubleClick(<span style="color:Blue; object sender, EventArgs e)
{
_points.Clear();
<span style="color:Blue; if (_rightButton && _points != <span style="color:Blue; null)
_points.Clear();

pictureBox1.Invalidate();
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_MouseUp(<span style="color:Blue; object sender, MouseEventArgs e)
{
_rightButton = <span style="color:Blue; false;
ResetCurrent();
}

<span style="color:Blue; private <span style="color:Blue; void ResetCurrent()
{
_current = <span style="color:Blue; null;

<span style="color:Blue; if (_points != <span style="color:Blue; null)
<span style="color:Blue; foreach (LinePoint lp <span style="color:Blue; in _points)
lp.IsCurrent = <span style="color:Blue; false;

<span style="color:Blue; this.pictureBox1.Invalidate();
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_MouseMove(<span style="color:Blue; object sender, MouseEventArgs e)
{
<span style="color:Blue; if (_current != <span style="color:Blue; null)
{
Point pt = _current.Point;
<span style="color:Blue; int x = _pt.X - pt.X;
<span style="color:Blue; int y = _pt.Y - pt.Y;
_current.Point = <span style="color:Blue; new Point(pt.X + x, pt.Y + y);

_pt = <span style="color:Blue; new Point(e.X, e.Y);

<span style="color:Blue; this.pictureBox1.Invalidate();
}

}

<span style="color:Blue; public <span style="color:Blue; class LinePoint
{
<span style="color:Blue; public Point Point { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public LinePoint Connection { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; bool Connected { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public Color ForeColor { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public Color HotColor { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; bool IsCurrent { <span style="color:Blue; get; <span style="color:Blue; set; }

<span style="color:Blue; public LinePoint()
{
ForeColor = Color.Red;
HotColor = Color.Blue;
}

<span style="color:Blue; public <span style="color:Blue; void Render(Graphics g, <span style="color:Blue; float circleWidth)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
<span style="color:Blue; using (SolidBrush myBrush = <span style="color:Blue; new SolidBrush((<span style="color:Blue; this.IsCurrent) ? HotColor : ForeColor))
g.FillEllipse(myBrush, <span style="color:Blue; new RectangleF(Point.X - circleWidth / 2F, Point.Y - circleWidth / 2F, circleWidth, circleWidth));

<span style="color:Blue; if (<span style="color:Blue; this.Connected && Connection != <span style="color:Blue; null)
g.DrawLine(Pens.Blue, Connection.Point, Point);
}

<span style="color:Blue; public <span style="color:Blue; bool HitTest(<span style="color:Blue; float x, <span style="color:Blue; float y, <span style="color:Blue; float circleWidth)
{
<span style="color:Blue; bool ret = <span style="color:Blue; false;
<span style="color:Blue; using (GraphicsPath gPath = <span style="color:Blue; new GraphicsPath())
{
gPath.AddEllipse(<span style="color:Blue; new RectangleF(Point.X - circleWidth / 2F, Point.Y - circleWidth / 2F, circleWidth, circleWidth));
ret = gPath.IsVisible(x, y);
}

<span style="color:Blue; return ret;
}
}
[/code]

Thanks.

<hr class="sig danieli

View the full article
 
Back
Top