I have some code to display video-like graphics of points moving around.
I am writing the points to a bitmap, and placing it on a picturebox.
The graphics computation has to be done on its own thread. The graphics work fine as long as you don't move the window around "too" much.
I'm using winforms. When I run the code, and move the window around wildly, I SOMETIMES get the following errors:
@ this.Invoke(d, new object[ ] { bmp }); "Cannot access a disposed object. Object name: 'Form1'."
@ gfx.DrawImage(bmpDestination, new Point()); "Object is currently in use elsewhere."
Here is the code:
private void button2_Click(object sender, EventArgs e)
{
Thread demoThread = new Thread(new ThreadStart(ThreadProcSafe));
demoThread.Start();
}
private void ThreadProcSafe()
{
creategraphics();
}
private void creategraphics()
{
Bitmap bmpDestination = new Bitmap(988, 588);
Bitmap bmp = new Bitmap(988, 588);
for (int i = 0; i < numtimesteps; i++)
{
bmp = GraphingUtility.create(apple, i, 988, 588, -30, 30, -30, 30);
using (Graphics gfx = Graphics.FromImage(bmp))
{
gfx.DrawImage(bmpDestination, new Point());
}
bmpDestination = bmp;
updateimage(bmp);
}
}
delegate void graphicscallback(Bitmap bmp);
private void updateimage(Bitmap bmp)
{
if (pictureBox1.InvokeRequired)
{
graphicscallback d = new graphicscallback(updateimage);
this.Invoke(d, new object[] { bmp });
}
else
{
pictureBox1.Image = bmp;
pictureBox1.Refresh();
}
}