[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to fix: Form that displays an animation clears the screen randomly?

Posted on 2006-04-06
9
Medium Priority
?
205 Views
Last Modified: 2011-09-20
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
      if (this.PaintWithBrush1)
        formGraphics.FillRectangle(pointBrush1, myPoint.X, myPoint.Y, 1, 1);
      else
        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.

Andy
0
Comment
Question by:AGBrown
6 Comments
 
LVL 9

Accepted Solution

by:
pallosp earned 1000 total points
ID: 16395940
I don't know the reason of this phenomenon, but as I know, all drawings to the screen should be done in the OnPaint() method. In the OnTimerFire() method Refresh() should be called to force repainting.

Nevertheless you can draw into a memory bitmap anywhere in the code.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 1000 total points
ID: 16396600
Its probably due to a Paint message being sent to your window. As pallosp said, you should really do all your drawing in the paint method. However, refreshes of the window can happen outside of the Paint event, you can prevent that by adding the following line to your forms constructor:

    this.SetStyle(ControlStyles.AllPaintingInWmPaint);
0
 
LVL 2

Expert Comment

by:kmaicorp
ID: 16396605
What kind of timer are you using? Are you using a System.Windows.Forms.Timer or a System.Timers.Timer? Perhaps this could be a threading issue?. But I agree with pallosp, you should update the UI on the paint method.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 12

Author Comment

by:AGBrown
ID: 16396642
I'll try moving the paints to the paint event and see what happens. carl_tawn, I've actually got this at the top of my initialisation (sorry I didn't put it in to start with):
    this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
    DoubleBuffered = true;
0
 
LVL 12

Author Comment

by:AGBrown
ID: 16430482
Just to let you know, I'm still struggling with trying to work out if this has worked or not. Hope to get back to you all soon.

A.
0
 
LVL 12

Author Comment

by:AGBrown
ID: 16622022
I've revived this project and I just spent an afternoon looking through it. The problem I've experienced is so intermittent that it's hard to tell if any one thing makes a difference.

For the people who have helped out; it would seem that moving the code I initially posted into a method, and calling that from OnPaint by making the timer Elapsed event call this.Refresh() did help. Interestingly I also realised that I was painting the form black in the Constructor for the form. Removing that altogether and keeping the method call to my first-post code in OnPaint does seem to have removed the problem.

So I now have a form without a background. The idea of this project was to help me get to grips with painting graphics on screen; unfortunately it seems to have left me with more questions than answers at the moment! I _think_ that we may have solved the problem, but I don't know enough to say if we have or not.

I would like to award points to pallosp (for pointing out the OnPaint idea) and carl_tawn (for confirming that I was right to put in my this.SetStyle call); does that seem reasonable?

Andy
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question