How to draw double buffered in C# or Java using GDI+

How to draw double buffered in C# or Java using GDI+

You draw something on a form using GDI+ or any other graphics library and try to update it with a timer. The problem is that the resulting display is flicking due to the lack of double buffering. Here is the solution. You have to draw on an image and then draw this image once using your Graphics object. This way you can obtain smooth display. Here is a simple code:

Graphics displayGraphics = e.Graphics;
Random r = new Random();
Image i = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
Graphics g = Graphics.FromImage(i);
g.FillRectangle(Brushes.White, ClientRectangle);
for (int x = 0; x < ClientRectangle.Width; x++)
{
    for (int y = 0; y < ClientRectangle.Height; y += 10)
    {
        Color c = Color.FromArgb(r.Next(25), r.Next(55), r.Next(5));
        Pen p = new Pen(c, 1);
        g.DrawLine(p, new Point(ClientRectangle.Width/2, ClientRectangle.Height/2), new Point(x, y));
        p.Dispose();
    }
}
displayGraphics.DrawImage(i, ClientRectangle);
i.Dispose();


added 11 years ago

- What is x-ray?
- The Wink home automation
- Skype works, shows I have internet, but browser wont go online
- If LED lights are so efficient then why do they get almost as hot as incandescents?
- How to correctly optimize your SSD for windows 10
- WAMP Mysqli: Your password has expired
- MySQL Dump/Restore, Dumping MySQL Database and Tables using MysqlDump command
- Source Code of Matrix Multiplication
- How to create your own PHP caching system? A simple example
- Domain Life Cycle
- Comparison of Random Functions According to their Exploration Performance (STL, .NET, Java)
- How to draw double buffered in C# or Java using GDI+
- How to recover Ubuntu after installing Windows using Ubuntu live cd
2
1