CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C# – Graphics – Double buffering

In our previous article, “C# – Graphics – Display an Image“, we have developed a C# program to display an image on the Windows Form. Have you observed, flickering when you move the window? OR when you click on the Draw button again and again? This is the common problem when we use a single buffer while rendering the Graphics objects on the controls. Through this article, we are going to discuss eliminating flickering during Graphics rendering.

A Device Context is associated with each control, and this is where paint operations will draw the graphics objects. Whatever we paint on the device context; is directly visible and this may cause flicker when we repeatedly paint or draw on the device context.

To eliminate this flicker, .Net provides, Double buffering concept; where the rendering of the graphics will happen in the back-end Memory instead of directly on the device context; and once the rendering is done in the Memory; the image will be displayed on the device context. As the rendering is happening in the background; and we see only the final drawing, once finished; we will not see the flickering of the drawing.

Double buffering eliminates the flickering. We can use .Net’s default buffering OR we can create our own buffering mechanism. Almost for all the painting operations; .Net’s default buffering should be enough.

Lets’ add Double buffering to our previously developed program in the Article “C# – Graphics – Display an Image“.

Enable double buffering

Step 1. Open our DrawImage project. And open Windows Form.

Step 2. Select the Form, and from the Properties dialog change the DoubleBuffered property to “True“. This enables double buffering on Windows Form. We set the property on this control because we are rendering the image on Windows form.

Set DoubleBuffered property on Windows Form
Set DoubleBuffered property on Windows Form

Step 3. Build and Run the program. And observe that, Visual Studio displays below Error message;

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ArgumentException: Parameter is not valid.
   at System.Drawing.Graphics.GetHdc()
   at System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer)
   at System.Drawing.BufferedGraphics.Render()
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

This is because there is an Error in the below code. Marked it in RED color; This is the code we have written through our previous Article.

        private void frmDrawImage_Paint(object sender, PaintEventArgs e)
        {
            if (img != null)
            {
                Graphics g = e.Graphics;

                g.DrawImage(img, 0, 0, this.Width, this.Height);

                g.Dispose();
            }
        }

Whenever we do with the Graphics objects, we must have to dispose of them by calling its’ Dispose method. But in this case, the Graphics object wasn’t created by our Program; it was created by the .Net framework. So, the program should not dispose of the object. Commenting or removing the g.Dispose(); statement will resolve the above issue.

Step 4. Now re-build and Run the program. We will not see the above Error, now.

Click on the Draw button to display an image on the form. Observe that, there is NO flickering while drawing the image. Keep on pressing, Draw button; and still NO flickering. This proves that Double buffering is working fine.

Draw Image - Windows Form
Draw Image – Windows Form

We will discuss more concepts through upcoming Articles.

If you like this article, please give your feedback through below Comments section.

(Raju)

C# – Graphics – Double buffering

One thought on “C# – Graphics – Double buffering

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top