Search code examples
bunifu

How can I avoid the flickering of gradient panel in winforms?


I have designed a dashboard like winform and when I try to resize the winform it flickers too much.

I have already tried SuspendLayout and enabled DoubleBufferring but still, the issue persists. Please check the following GIF.

WinForm flickring While Resizing GIF

EDIT

Here is the code for Gradient Panel:

 this.bunifuGradientPanel1.BackColor = System.Drawing.Color.Transparent;
            this.bunifuGradientPanel1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("bunifuGradientPanel1.BackgroundImage")));
            this.bunifuGradientPanel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.bunifuGradientPanel1.Controls.Add(this.panel1);
            this.bunifuGradientPanel1.Controls.Add(this.panel4);
            this.bunifuGradientPanel1.Controls.Add(this.panel3);
            this.bunifuGradientPanel1.Controls.Add(this.panel5);
            this.bunifuGradientPanel1.Controls.Add(this.panel6);
            this.bunifuGradientPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.bunifuGradientPanel1.GradientBottomLeft = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            this.bunifuGradientPanel1.GradientBottomRight = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
            this.bunifuGradientPanel1.GradientTopLeft = System.Drawing.Color.Purple;
            this.bunifuGradientPanel1.GradientTopRight = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128)))));
            this.bunifuGradientPanel1.Location = new System.Drawing.Point(0, 0);
            this.bunifuGradientPanel1.Name = "bunifuGradientPanel1";
            this.bunifuGradientPanel1.Quality = 10;
            this.bunifuGradientPanel1.Size = new System.Drawing.Size(1020, 680);
            this.bunifuGradientPanel1.TabIndex = 0;

Thank you for your help in advance.


Solution

  • Finally solved this issue!

    Here is the correct answer just in case anyone may face this problem in the future:

    First, create the following function inside your Form.cs:

    //Double Buffering Function
            public static void SetDoubleBuffering(System.Windows.Forms.Control control, bool value)
            {
                System.Reflection.PropertyInfo controlProperty = typeof(System.Windows.Forms.Control)
                    .GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                controlProperty.SetValue(control, value, null);
            }
    

    Then simply call the function in your Form1_Load function and pass the name of the control (panel in my case 'bunifuGradientPanel1') and you're ready to go:

     private void Form1_Load(object sender, EventArgs e)
            {
                // Enabling Double Buffering for BunifuGradientPanel
                SetDoubleBuffering(bunifuGradientPanel1, true);
            }
    

    Hope this helps anyone facing the flickering issue while working with Panels in Windows Forms c#.

    Thanks to Fluxbytes.com for creating the function above and posting it on their forum.