1

So I have a custom WF control with some shapes and text in it. Those objects tend to move frequently. Since nothing like this.DoubleBuffered = true or "WS_EX_COMPOSITED" works (at least for me) to get rid of flickering, I managed to create a very simple double buffering myself. The problem is that rendering is VERY slow. It's fast enough only on small resolution.

It works like this:

    Graphics g;
    Bitmap buffer;

    public SomeControl()
    {
        InitializeComponent();
        buffer = new Bitmap(this.Width, this.Height);
        g = Graphics.FromImage(buffer);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        g.Clear(BackColor);
        //some painting with "g"
        e.Graphics.DrawImageUnscaled(buffer, Point.Empty);
    }

    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        buffer = new Bitmap(this.Width, this.Height);
        g = Graphics.FromImage(buffer);
    }

    void Repaint()
    {
        this.InvokePaint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        //Move something around
        Repaint();
    }

If I don't draw it with "g" but with "e.Graphics" instead, it's fast enough (but it flickers). So, is there a faster way to do double buffering with WF controls? Thanks!

(Also sorry if my english is bad)

JTaylor
  • 13
  • 2
  • Shouldn't the whole commented out painting happen in the "Repaint" function? After all, you don't need to redraw your buffer if nothing changed. – nvoigt Jan 08 '14 at 17:32
  • It would help to know more about what `SomeControl` is. And why `DoubleBuffered = true` didn't work. It should. You mention the Win32 flag, which is sometimes a necessary alternative when `DoubleBuffered` is not exposed. Either way, you should not have to handle the buffered drawing yourself. – DonBoitnott Jan 08 '14 at 19:22
  • @DonBoitnott This "SomeControl" shows how pretty much all of my custom controls are based. For Instance, an editor that needs to draw a grid and some rectangles and so on. This time I just wanted to make something like SplitContainer but with realtime resizing. But even drawing a single rectangle as a splitter which I dragged with mouse either flickers or renders slowly (like few frames per second). – JTaylor Jan 08 '14 at 19:48
  • Nearly impossible to answer without seeing more of it. But I suspect that you're causing something unexpected to refresh. Possibly something databound, or that has to fetch data? Guessing. But likely that there's more happening than is obvious. – DonBoitnott Jan 08 '14 at 20:26

0 Answers0