-1

Possible Duplicates:
How do I double buffer a Panel in C#?
c# panel for drawing graphics and scrolling

I draw a bitmap on a panel, i use zooming on the same panel. While zooming the panel is continuously flickering. Why do not panel have the DoubleBuffered property?

Code:

                Graphics g = Graphics.FromHwnd(panel.Handle);
                if (newImage == true)
                {
                    g.Clear(SystemColors.Control);
                    newImage = false;
                }

                g.DrawImage(bmp, hOffset, vOffset);
                g.Dispose();
Community
  • 1
  • 1
y_zyx
  • 582
  • 4
  • 10
  • 28

4 Answers4

1

use this.

System.Drawing.BufferedGraphics

I am a game developer.In games we first draw all objects in a backbuffer and then copy or flip it to frontbuffer.You can use

System.Drawing.BufferedGraphics

as backbuffer and render it to graphics object.

System.Drawing.Graphics

for example:

        System.Drawing.Graphics g = this.CreateGraphics();
        System.Drawing.BufferedGraphicsContext dc = new BufferedGraphicsContext();
        BufferedGraphics backbuffer = dc.Allocate(g, new Rectangle(new Point(0, 0), g.VisibleClipBounds.Size.ToSize()));
        backbuffer.Graphics.DrawImage(Image.FromFile(@"c:\test.jpg"), new Point(10, 10));
        backbuffer.Render(g);
Saleh
  • 2,982
  • 5
  • 34
  • 59
1

Add this code inside the constructor

this.SetStyle(ControlStyles.AllPaintingInWmPaint |ControlStyles.UserPaint |ControlStyles.DoubleBuffer, true);
Renju Vinod
  • 254
  • 1
  • 3
  • 11
  • Inside of *which* constructor? Double-buffering the form isn't going to have any effect on flicker in a panel control on that form. You'll have to subclass the `Panel` class and place this code in the constructor for your custom class. Absent more information, this isn't a very helpful answer. – Cody Gray - on strike May 11 '11 at 11:54
0

Where are you painting the bitmap?

If not in the Paint event or OnPaint override, then it is wrong.

To answer your question, only forms have the DoubleBuffered property, IIRC.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • I use the Paint event. :). Then how can I avoid the flickering? – y_zyx May 11 '11 at 11:46
  • if you do all the drawing on your own you could create your own double buffering, Create a bitmap with the same size as the panel and draw on that one then draw the bitmap to the panel.. – Peter May 11 '11 at 11:49
-2

Im not 100% sure but cant you activate DoubleBuffered on the form/window instead?

And one tip if your going to use gui with alot of effects i would go with WPF instead of winforms..

You can also override OnPaint and OnPaintBackground..

Peter
  • 37,042
  • 39
  • 142
  • 198
  • Making the form/window double-buffered isn't going to do *anything* for the controls on that form/window. I don't think zooming in on an image counts as "a lot of effects"; WinForms is just fine for this. What is the advantage of overriding `OnPaint` and `OnPaintBackground` here? – Cody Gray - on strike May 11 '11 at 11:49
  • look at the comment i posted on @leppie post... – Peter May 11 '11 at 14:31
  • 1
    Comments made on another answer do surprisingly little to improve your *own* answer. – Cody Gray - on strike May 12 '11 at 03:33