![]() |
Multimedia »
GDI+ »
General
Intermediate
Anti Flicker Graphics using Double Buffering and how to make simple graphic moves.By MeterManThis 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
|
|
Advanced Search |
|
|
|
||||||||||||||||

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.
Timer and drag it to the WinForm.
Timer and hit Properties.
enabled is set to true.
Interval to 1.
moveme.
if(CarsXValue>0)
CarsXValue-=Carspeed;
else
{
CarsXValue=672;
}
Invalidate();Now we need to make a paint event that will draw our images for us. So we do the following:
form1() constructor: Paint += new PaintEventHandler(OnPaint);
Onpaint event by doing the following. void OnPaint(Object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
DrawCar(g);
}
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
}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.
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**********
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);
Again make sure that the fastcar.gif is in your debug or release folder depending on which build configuration you have selected.
I hope this helps you as much as it did me.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 |