Double Buffering when not drawing in OnPaint(): why doesn’t it work?

g = doc.CreateGraphics();

That’s the mistake. Double-buffering can only work if you draw into the buffer. The one that e.Graphics references. Fix:

g = e.Graphics;

Beware that Panel doesn’t have double-buffering turned on by default. You’ll need to derive your own. Paste this into a new class:

using System;
using System.Windows.Forms;

class BufferedPanel : Panel {
    public BufferedPanel() {
        this.DoubleBuffered = true;
        this.ResizeRedraw = true;
    }
}

Compile. Drop it from the top of the toolbox.

Leave a Comment