Click here to Skip to main content
6,305,776 members and growing! (17,526 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate

Anti Flicker Graphics using Double Buffering and how to make simple graphic moves.

By MeterMan

This example will show you how to use doublebuffering and hopefully allows you to make images move across your screen.
C#.NET 1.0, Win2K, WinXP, Win2003, Dev
Posted:12 May 2004
Views:53,627
Bookmarked:17 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
21 votes for this article.
Popularity: 3.61 Rating: 2.73 out of 5
7 votes, 33.3%
1
1 vote, 4.8%
2
4 votes, 19.0%
3
2 votes, 9.5%
4
7 votes, 33.3%
5

Sample Image - screenshot.jpg

Introduction

While writing a game which I will hopefully release later on here, I had to find a way to reduce flicker. I also ran into the problem of the redraws causing the game to move extremely slow. I did some research and found out about DoubleBuffering, Timers and PaintEvents. I hope you learn from this as much as I did.

Step1: Making the Timer

  1. Go to the toolbar after you have made the basic WinForm skeleton. Select Timer and drag it to the WinForm.
  2. Right click on the Timer and hit Properties.
    1. Make sure that enabled is set to true.
    2. Make the Interval to 1.
    3. Click on the the Lightening bolt (event maker).
      • Where you see tick, add an event named moveme.
      • Now once we have an event, it will take you to the code block.
      • Add the following:
        if(CarsXValue>0)
            CarsXValue-=Carspeed;
        else
        {
            CarsXValue=672;
        }
        Invalidate();

Step2: Make a Paint Event

Now we need to make a paint event that will draw our images for us. So we do the following:

  1. Include the following in the form1() constructor:
    Paint += new PaintEventHandler(OnPaint);
  2. Then you need to actually make the Onpaint event by doing the following.
    void OnPaint(Object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
    
        DrawCar(g);
    }
  3. Make a DrawCar function that will draw the images for us:
    void DrawCar(Graphics g)
    {
        g.DrawImage(FastCar,CarsXValue,672);
    //draw the image using the image fastcar
    
    //its x value
    
    //and a hardcoded y value
    
    }

Step 3: Make Our Variables

Now we need some variables to control our movement. We also need an image to draw and a X co-ordinate that I will explain.

  1. Make the following variables where all the other main variables are declared.
    int CarsXValue=0; // x cordinate used for the car
    
    int CarSpeed=2; // how fast it will move across the screen
    
    Image FastCar=Image.FromFile("fastcar.gif"); // our car image

*****Make sure the FASTCAR.GIF is in the debug or release folder**********

Step 4: Turn on Double Buffering

We now turn on double buffering to prevent flicker. Include the following code in the Form1() constructor. And you're all set.

   SetStyle(ControlStyles.UserPaint, true);
   SetStyle(ControlStyles.AllPaintingInWmPaint, true);
   SetStyle(ControlStyles.DoubleBuffer, true);
   SetStyle(ControlStyles.UserPaint, true);
   SetStyle(ControlStyles.AllPaintingInWmPaint, true);
   SetStyle(ControlStyles.DoubleBuffer, true);

Step 5: Compile and Run

Again make sure that the fastcar.gif is in your debug or release folder depending on which build configuration you have selected.

Conclusion:

I hope this helps you as much as it did me.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

MeterMan


Member
Graduated from Kennesaw State with a Computer Informatin Systems degree. I have a fair knowledge of C++ and C# but by far am not an expert. My skills are more into software troubleshooting and repair.

Currently working as an IT Specialist for a Radio Read Meter Company.
Location: United States United States

Other popular GDI+ articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralDoes this apply to controls within the form? PinmemberMWBate17:44 22 Jun '09  
GeneralSpam charater Pinmemberthechien21:56 2 Dec '08  
GeneralReally Helpful PinmemberMohammad Ali Azam21:37 14 May '08  
GeneralThank You PinsussBrent200721:41 4 Dec '07  
Generalprevent the flickering while onpaint event Pinmemberstallion_the_black4:52 21 Feb '07  
Generalhamed : i am iranian Pinmemberhamed_646:18 13 Aug '06  
QuestionCarsXValue haven't display on the watch when debugging !!! Pinmemberjobofmoses19:59 29 Mar '06  
Questionrun successfully .... Pinmemberjobofmoses9:16 29 Mar '06  
Generalwatch out for using menu controls PinmemberRuud van Eeghem5:04 6 Jan '06  
Generalthanks! PinmemberPn0y6i912:27 13 Dec '05  
Generalrepeating code PinmemberNetDevKing7:55 24 May '04  
GeneralRe: repeating code Pinmemberwin32newb9:48 24 May '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 May 2004
Editor: Smitha Vijayan
Copyright 2004 by MeterMan
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project