Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to work with Double Buffering C#

How to work with Double Buffering C#

Scheduled Pinned Locked Moved C#
csharpvisual-studiographicsarchitecturehelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    laura1316
    wrote on last edited by
    #1

    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

    D L P 3 Replies Last reply
    0
    • L laura1316

      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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Well, 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 your Pen 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

      1 Reply Last reply
      0
      • L laura1316

        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

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, 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]

        1 Reply Last reply
        0
        • L laura1316

          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

          P Offline
          P Offline
          Patrick Etc
          wrote on last edited by
          #4

          Laura, 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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups