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

后端 未结 6 1568
终归单人心
终归单人心 2020-12-08 02:33

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

相关标签:
6条回答
  • 2020-12-08 03:10

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

    0 讨论(0)
  • 2020-12-08 03:11

    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.

    0 讨论(0)
  • 2020-12-08 03:13

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

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

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

    My images were not huge,but somewhat oversized.

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

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

    0 讨论(0)
  • 2020-12-08 03:19

    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.

    0 讨论(0)
  • 2020-12-08 03:21

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

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

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

    0 讨论(0)
  • 2020-12-08 03:25

    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;
    
    0 讨论(0)
提交回复
热议问题