How to work with Double Buffering C#
-
Hi All: I am trying to work with the built in double buffering in C# Visual Studio 2005. I want to have no flicker. I have a timer event that throws an event every 100 ms when the timer event occurs it draws a square at the given location. This occurs very quickly. Behind the square is a four quadrant background. I am trying to do the double buffering so that the background is not redrawn everytime or so that it is smoother. When I set the doublebuffer function to true it flickers faster so I am obviously not doing something right. Please help! Please view my Paint function:
public void XYZ_PaintData(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Pen penRed = new Pen(Color.Red, 2); Pen penGreen = new Pen(Color.Green, 2); Pen penBlue = new Pen(Color.Blue, 2); Pen penWhite = new Pen(Color.White, 1); Pen penBack = new Pen(Color.Gray, 1); //Graphics g = this.CreateGraphics(); //g.Clear(this.BackColor); this.DoubleBuffered = true; /*Pen penRed = new Pen(Color.Red, 2); Pen penGreen = new Pen(Color.Green, 2); Pen penBlue = new Pen(Color.Blue, 2); Pen penWhite = new Pen(Color.White, 1); Pen penBack = new Pen(Color.Gray, 1);*/ penBack.DashStyle = DashStyle.Solid; // Background g.DrawLine(penBack, 375, 105, 375, 600); g.DrawLine(penBack, 50, 325, 650, 325); g.DrawRectangle(penBack, x0XYZ, y0XYZ, x1XYZ, y1XYZ); AccelReadings = true; //Line that is drawn when timer event occurs g.DrawRectangle(Pens.Black, xMXYZ, yMXYZ, 2, 2); }
Laura -
Hi All: I am trying to work with the built in double buffering in C# Visual Studio 2005. I want to have no flicker. I have a timer event that throws an event every 100 ms when the timer event occurs it draws a square at the given location. This occurs very quickly. Behind the square is a four quadrant background. I am trying to do the double buffering so that the background is not redrawn everytime or so that it is smoother. When I set the doublebuffer function to true it flickers faster so I am obviously not doing something right. Please help! Please view my Paint function:
public void XYZ_PaintData(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Pen penRed = new Pen(Color.Red, 2); Pen penGreen = new Pen(Color.Green, 2); Pen penBlue = new Pen(Color.Blue, 2); Pen penWhite = new Pen(Color.White, 1); Pen penBack = new Pen(Color.Gray, 1); //Graphics g = this.CreateGraphics(); //g.Clear(this.BackColor); this.DoubleBuffered = true; /*Pen penRed = new Pen(Color.Red, 2); Pen penGreen = new Pen(Color.Green, 2); Pen penBlue = new Pen(Color.Blue, 2); Pen penWhite = new Pen(Color.White, 1); Pen penBack = new Pen(Color.Gray, 1);*/ penBack.DashStyle = DashStyle.Solid; // Background g.DrawLine(penBack, 375, 105, 375, 600); g.DrawLine(penBack, 50, 325, 650, 325); g.DrawRectangle(penBack, x0XYZ, y0XYZ, x1XYZ, y1XYZ); AccelReadings = true; //Line that is drawn when timer event occurs g.DrawRectangle(Pens.Black, xMXYZ, yMXYZ, 2, 2); }
LauraWell, I don't know what's causing the flickering, but I do see a HUGE resource leak in your code. You MUST call
.Dispose()
on all of yourPen
objects when you're done using them. If you don't, the unmanaged resources behind them don't get released and you'll eventually get an OutOfMemory exception and/or your system will start to do very weird things, even causing other applications to crash.Dave Kreskowiak Microsoft MVP - Visual Basic
-
Hi All: I am trying to work with the built in double buffering in C# Visual Studio 2005. I want to have no flicker. I have a timer event that throws an event every 100 ms when the timer event occurs it draws a square at the given location. This occurs very quickly. Behind the square is a four quadrant background. I am trying to do the double buffering so that the background is not redrawn everytime or so that it is smoother. When I set the doublebuffer function to true it flickers faster so I am obviously not doing something right. Please help! Please view my Paint function:
public void XYZ_PaintData(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Pen penRed = new Pen(Color.Red, 2); Pen penGreen = new Pen(Color.Green, 2); Pen penBlue = new Pen(Color.Blue, 2); Pen penWhite = new Pen(Color.White, 1); Pen penBack = new Pen(Color.Gray, 1); //Graphics g = this.CreateGraphics(); //g.Clear(this.BackColor); this.DoubleBuffered = true; /*Pen penRed = new Pen(Color.Red, 2); Pen penGreen = new Pen(Color.Green, 2); Pen penBlue = new Pen(Color.Blue, 2); Pen penWhite = new Pen(Color.White, 1); Pen penBack = new Pen(Color.Gray, 1);*/ penBack.DashStyle = DashStyle.Solid; // Background g.DrawLine(penBack, 375, 105, 375, 600); g.DrawLine(penBack, 50, 325, 650, 325); g.DrawRectangle(penBack, x0XYZ, y0XYZ, x1XYZ, y1XYZ); AccelReadings = true; //Line that is drawn when timer event occurs g.DrawRectangle(Pens.Black, xMXYZ, yMXYZ, 2, 2); }
LauraHi, I would set DoubleBuffered only once, probably in the constructor. Maybe setting it every time causes the flickering you see. And you either should make your pens class members (so they get created only once), or Dispose of them every time you create them (wasting CPU cycles in creation, disposal and garbage collection). I am not sure what "AccelReadings=true" is supposed to do there. Seems not to belong in a paint handler. Final remark: the comment "Line that is drawn when timer event occurs" is inaccurate, the line is drawn every time paint is executed (which could be on every time tick and more), it probably gets instructed to move in the time tick handler. Hope this helps.
Luc Pattyn [My Articles]
-
Hi All: I am trying to work with the built in double buffering in C# Visual Studio 2005. I want to have no flicker. I have a timer event that throws an event every 100 ms when the timer event occurs it draws a square at the given location. This occurs very quickly. Behind the square is a four quadrant background. I am trying to do the double buffering so that the background is not redrawn everytime or so that it is smoother. When I set the doublebuffer function to true it flickers faster so I am obviously not doing something right. Please help! Please view my Paint function:
public void XYZ_PaintData(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Pen penRed = new Pen(Color.Red, 2); Pen penGreen = new Pen(Color.Green, 2); Pen penBlue = new Pen(Color.Blue, 2); Pen penWhite = new Pen(Color.White, 1); Pen penBack = new Pen(Color.Gray, 1); //Graphics g = this.CreateGraphics(); //g.Clear(this.BackColor); this.DoubleBuffered = true; /*Pen penRed = new Pen(Color.Red, 2); Pen penGreen = new Pen(Color.Green, 2); Pen penBlue = new Pen(Color.Blue, 2); Pen penWhite = new Pen(Color.White, 1); Pen penBack = new Pen(Color.Gray, 1);*/ penBack.DashStyle = DashStyle.Solid; // Background g.DrawLine(penBack, 375, 105, 375, 600); g.DrawLine(penBack, 50, 325, 650, 325); g.DrawRectangle(penBack, x0XYZ, y0XYZ, x1XYZ, y1XYZ); AccelReadings = true; //Line that is drawn when timer event occurs g.DrawRectangle(Pens.Black, xMXYZ, yMXYZ, 2, 2); }
LauraLaura, Another thing you can try is your own simple double buffering: Paint everything to a temporary bitmap and then paint the bitmap to the graphics passed by the event args, like so: Bitmap temp = new Bitmap(e.ClipRectangle); // do your painting e.Graphics.DrawImageUnscaled(temp, e.ClipRectangle); That may help. Incidentally, are you using transparency of any kind? Transparency can cause flickering despite double buffering.