[ACCEPTED]-Windows Forms: using BackgroundImage slows down drawing of the Form's controls-performance

Accepted answer
Score: 47

SuspendLayout() and ResumeLayout() do not suspend drawing, only layout 5 operations. Give this guy a shot:

public static class ControlHelper
{
    #region Redraw Suspend/Resume
    [DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
    private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
    private const int WM_SETREDRAW = 0xB;

    public static void SuspendDrawing(this Control target)
    {
        SendMessage(target.Handle, WM_SETREDRAW, 0, 0);
    }

    public static void ResumeDrawing(this Control target) { ResumeDrawing(target, true); }
    public static void ResumeDrawing(this Control target, bool redraw)
    {
        SendMessage(target.Handle, WM_SETREDRAW, 1, 0);

        if (redraw)
        {
            target.Refresh();
        }
    }
    #endregion
}

Usage should 4 be pretty self-explanatory, and the syntax 3 is identical to SuspendLayout() and ResumeLayout(). These are extension 2 methods that will show on any instance of 1 Control.

Score: 5

I also faced the same problem and could 8 solve it by reducing the resolution of the 7 background picture. When you use big sized 6 (eg:1280X800) pictures as the background, it 5 will take time to draw controls on the form. It 4 is better to open the picture in 'Paint' re 3 size it smaller than your form and then 2 save in 'bmp' format. Now try to add this 1 picture as your form's background.

Score: 4

Another very simple way to avoid permanent 5 redraws while adding your controls is to 4 make the parent control invisible before 3 you add controls to it. Afterwards you make 2 the parent control (for example, a panel) visible 1 and there it is without all those repaints. :-)

panelParent.visible = false;

for(...) {
    // Add your controls here:
    panelParent.Controls.Add(...);
}

panelParent.visible = true;
Score: 3

For me, the post Form load is slow if adding a background image solved the problem:

Make 5 sure your backgroundcolor is not set to 4 'transparent'. Set it to 'white' for better 3 performance.

Also, do BackgroundImageLayout to be either 'Center' or 2 'Stretch' to increase performance. This will enable 1 the double buffer on the form.

Score: 3

I solved same problem using PictureBox. Just add PictureBox to 4 your container, choose "Dock it in parent 3 container" (or property Dock = Fill) and set it's 2 Image. It will looks just like BackGroundImage 1 of the parent control.

Score: 1

I realize this is an old thread, but I found 28 it while searching for info on the same 27 issue, so in case it's useful to someone 26 at some point:

My situation: I have a 13 25 x 12 grid of panels, which have a background 24 image set dynamically, and regularly changed, based 23 on user selections. Each Panel also has 22 a text label control added to it. In order 21 for the text to overlay the background image, it 20 has to be set to Transparent (btw - my experience 19 was that BackgroundImageLayout of Zoom, Stretch, Center 18 had little to no effect. BackColor set 17 to transparent or white also had little 16 effect).

Each draw of the grid of panels 15 (including resizing) was taking about a 14 second - not bad but very visible, and a 13 usability issue on slower machines.

My images 12 were not huge,but somewhat oversized.

What 11 I found: By resizing my set of images to 10 the exact panel size before setting the 9 backgroundimage, the draw time went down 8 dramatically - somewhere around 0.1 seconds. Since 7 my program dynamically resizes panels based 6 on windows size, I dynamically resize the 5 set of images one time on windows resize 4 event before setting the background of the 3 156 panels.

In hindsight, its an obvious 2 optimization... resize 8 images once instead 1 of repeatedly 156 times.

More Related questions