Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms: using BackgroundImage slows down drawing of the Form's controls

I have a Windows Form (C# .NET 3.5) with a number of buttons and other controls on it, all assigned to a topmost Panel which spans the whole Form. For example, the hierarchy is: Form -> Panel -> other controls.

As soon as I assign a BackgroundImage to the Panel, the controls draw very slowly. I have the same effect if I use the Form's BackgroundImage property and set the Panel's BackgroundColor to "transparent". It appears as if the window with the background is drawn first, then each control is added one-by-one each with a slight delay before the next is drawn. In other words, you can actually follow the order in which each control is drawn to the Form. Once all Controls have been drawn once this effect doesn't happen anymore but the responsiveness of the Form is still slow.

In Visual Studio's designer I get the same effect, especially noticeable when moving controls around. Sometimes the form's drawing stops completely for a second or two which makes working with BackgroundImage a total drag, both in the designer and the resulting application.

Of course, I tried using DoubleBuffered = true, and I also set it on all controls using reflection, to no effect.

Also, here's the forms loading code because it's a bit unusual. It copies all controls from another form onto the current form. This is done in order to be able to edit each screen's visual appearance separately using the designer while sharing a common form and common code basis. I have a hunch that it may be the cause of the slowdowns, but it still doesn't explain why the slowdowns are already noticeable in the designer.

private void LoadControls(Form form)
{
    this.SuspendLayout();

    this.DoubleBuffered = true;
    EnableDoubleBuffering(this.Controls);

    this.BackgroundImage = form.BackgroundImage;
    this.BackColor = form.BackColor;

    this.Controls.Clear();
    foreach (Control c in form.Controls)
    {
        this.Controls.Add(c);
    }

    this.ResumeLayout();
}

As you can see, SuspendLayout() and ResumeLayout() are used to avoid unnecessary redraw.

Still, the form is "slow as hell" once a BackgroundImage is used. I even tried converting it to PNG, JPG and BMP to see if that makes any difference. Also, the image is 1024x768 in size, but smaller images have the same slowdown effect (although slightly less).

What should I do?

like image 730
steffenj Avatar asked Apr 22 '09 16:04

steffenj


People also ask

What Windows form controls?

Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client-side, Windows-based applications. Not only does Windows Forms provide many ready-to-use controls, it also provides the infrastructure for developing your own controls.

Is WinForms going away?

As we mentioned above, WinForms is still available but the status of “maintenance mode” likely means it has no long term future. As time passed by, especially in the last 5-10 years, new tools continued to mature and rise in popularity, and each one of them offered many powerful features.


3 Answers

SuspendLayout() and ResumeLayout() do not suspend drawing, only layout 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 be pretty self-explanatory, and the syntax is identical to SuspendLayout() and ResumeLayout(). These are extension methods that will show on any instance of Control.

like image 86
Adam Robinson Avatar answered Sep 25 '22 08:09

Adam Robinson


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

like image 44
Sheenit Mathew Avatar answered Sep 22 '22 08:09

Sheenit Mathew


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

panelParent.visible = false;

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

panelParent.visible = true;
like image 22
Martin Möller Avatar answered Sep 24 '22 08:09

Martin Möller