none
C# animation

    Question

  • Is there anything out there that will allow me to do simple drawing and animation in c#? I was thinking about creating some programs that drew circles and had them move in ellipses and with gravity and that sort of stuff.
    Monday, August 04, 2008 4:26 PM

Answers

  • Don't use a loop and sleep. It will freeze your application (appart from the animation) so you can't close or move your form.

    Instead use a timer and in the timer event call Invalidate() which tells the framework that the form needs to be repainted and will eventually call the Paint event handler. In the Paint event handler you should handle all your painting. You should also update the position and form of your graphics objects to be animated in the timer event handler.

    The position and form of the graphics objects could be made into separate classes, which makes it easy to keep these properties in one place per object between the timer events. Put the objects in a list and loop through the list, first to update the position in the timer event and then to repaint them in the Paint event. New objects can easily be added and existing ones can be removed.



    /Ruben RJJournal
    Monday, August 04, 2008 9:33 PM
  • Do as max says above. For smoother animation in the form options you may want to turn on Double Buffering. It will make it so the form won't flicker when the graphics are updated. It actually can give a very smooth animation on a form with just regular graphics.

    There are many tutorials out there for drawing in regular C#. They will be under GDI+, thats what its called.


    Tuesday, August 05, 2008 12:46 AM
  • Yes, double buffering will sometimes make things easier. Draw in an offscreen bitmap and in the Paint event copy the offscreen bitmap to the form. This way, the drawing can also be done in the timer event instead of the paint event.

    Here is an example of how to draw to a picture box (instead of directly to the form background) using an offscreen bitmap for double buffering: http://forums.msdn.microsoft.com/en-US/csharpgeneral/thread/91fb16c7-d1d9-46bf-8bc1-5d095a27e0ba/

    (my post a bit down in the thread).


    /Ruben RJJournal
    Tuesday, August 05, 2008 6:10 AM

All replies

  • The Graphics class has everything you need.  Don't expect Pixar quality rendering effects and speed.
    Hans Passant.
    Monday, August 04, 2008 6:13 PM
    Moderator
  • Use can find in graphics namespace....

    I you are more interested towards graphics... have a look on XNA (gameengine from MS) and look at some DirectX stuff...
    krishna
    Monday, August 04, 2008 6:23 PM
  • Krishna, I couldnt find any info on the graphics namespace. I have found this tutorial on the system.drawing namespace (http://www.geekpedia.com/tutorial50_Drawing-with-Csharp.html) whichshows how to make some graphics. Now If I wanted to animated these drawing, what would i need to do? put it all in a loop and usleep(1./30.*1E6) (for 30 fps animation)?

    Im looking for really simple stuff not directX 3d highspeed type of animations. Kind of like what people used to do in basic.
    Monday, August 04, 2008 9:05 PM
  • Don't use a loop and sleep. It will freeze your application (appart from the animation) so you can't close or move your form.

    Instead use a timer and in the timer event call Invalidate() which tells the framework that the form needs to be repainted and will eventually call the Paint event handler. In the Paint event handler you should handle all your painting. You should also update the position and form of your graphics objects to be animated in the timer event handler.

    The position and form of the graphics objects could be made into separate classes, which makes it easy to keep these properties in one place per object between the timer events. Put the objects in a list and loop through the list, first to update the position in the timer event and then to repaint them in the Paint event. New objects can easily be added and existing ones can be removed.



    /Ruben RJJournal
    Monday, August 04, 2008 9:33 PM
  • Do as max says above. For smoother animation in the form options you may want to turn on Double Buffering. It will make it so the form won't flicker when the graphics are updated. It actually can give a very smooth animation on a form with just regular graphics.

    There are many tutorials out there for drawing in regular C#. They will be under GDI+, thats what its called.


    Tuesday, August 05, 2008 12:46 AM
  • Yes, double buffering will sometimes make things easier. Draw in an offscreen bitmap and in the Paint event copy the offscreen bitmap to the form. This way, the drawing can also be done in the timer event instead of the paint event.

    Here is an example of how to draw to a picture box (instead of directly to the form background) using an offscreen bitmap for double buffering: http://forums.msdn.microsoft.com/en-US/csharpgeneral/thread/91fb16c7-d1d9-46bf-8bc1-5d095a27e0ba/

    (my post a bit down in the thread).


    /Ruben RJJournal
    Tuesday, August 05, 2008 6:10 AM