I'm using GDI+ through C# in .NET 2.0 via the System.Drawing classes. I have a single form which I black out and use as a canvas to animate pixels; effectively a number of pixels are turned to a different colour using (more or less) the following code (the form is using DoubleBuffering):
Graphics formGraphics; // class field - set from this.CreateGraphics() on initialisation
LinearGradientBrush pointBrush1; // class field - set on initialisation
LinearGradientBrush pointBrush2; // class field - set on initialisation
OnTimerFire () // timer period is 1 millisecond
Timer1.Enabled = false;
for (int i = 0; i < iterationsPerTimerFire; i++)
Point myPoint; // set in another method
formGraphics.FillRectangle(pointBrush1, myPoint.X, myPoint.Y, 1, 1);
formGraphics.FillRectangle(pointBrush2, myPoint.X, myPoint.Y, 1, 1);
Timer1.Enabled = true;
However, periodically (and randomly in terms of the time since the last time it happened) the form goes black again, and all the pixels that were coloured in are turned back to black. This can happen after only half a second, or after two minutes or any time inbetween, and will happen repeatedly during each animation. I estimate I'm drawing about 50,000 to 100,000 pixels a second at full speed, though it also happens when it runs slower, and the length of time between "resets" doesn't seem to correlate to the number of pixels drawn.
How can I prevent this? The form is running in maximised mode, but I think I've seen the same behaviour in normal mode as well - just not recently as I've had it in maximised mode for a while now.