VB.NET Form with Background Image Flickers and Slows on Resize

When you set a background image to a form, resizing the form may end up slow and flickers regardless of the image size.

Three suggested solutions found online.

1.) PixelFormat.Format32bppPArgb


Load the background image dynamically (on run-time) whenever the form object is instantiated, do not reference the background image on design-time.

Then, use the technique of rendering a bitmap with PixelFormat.Format32bppPArgb to load the background image.

I do not witness significant performance gain deploying this technique as compared to conventional method and it doesn't solve the flickering issue.

Public Overrides Property BackgroundImage() As System.Drawing.Image
            Return Me.RenderBmp
        End Get
        Set(ByVal value As System.Drawing.Image)
            If Not value Is Nothing Then
                Me.RenderBmp = New Bitmap(Me.Width, Me.Height, _
                Dim g As Graphics = Graphics.FromImage(Me.RenderBmp)
                g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.High
                g.DrawImage(value, New Rectangle(0, 0, Me.RenderBmp.Width, _
            End If 'If Not value Is Nothing
        End Set
    End Property

2.) DoubleBuffered

The following URL also suggested usage of Double Buffering technique to optimize performance of loading forms with images and many controls.


Nevertheless, I found that it didn't help with the fickering issue.

I guess the reason is that Standard system.windows.forms.form controls are double buffered by default as stated in the following URL.


3.) Extended Windows Style

What really works to solve the flickering issue can be found from the following URL.


Just need to include the following codes in the form - basically the codes overrides the CreateParams property of your system.windows.forms.form.

Protected Overrides ReadOnly Property CreateParams() As Windows.Forms.CreateParams
        Dim cp As CreateParams = MyBase.CreateParams
        cp.ExStyle = cp.ExStyle Or &H2000000
        Return cp
    End Get
End Property





Now, one may wonder how this 'magic' code really works ?

Basically, what it does is to overrides the value of cp.ExStyle by performing a bitwise OR with the value &H2000000.

Hence, what does &H2000000 means ?

According to the following URL, it means WS_CLIPCHILDREN: Excludes the area occupied by child windows when drawing occurs within the parent window. This style is used when creating the parent window.


What I discern from the definition is that when this value is set for extended windows style, during the event of re-drawing (the background image), all child controls will be excluded from the drawing (of the background image), only the parent windows (in this case the form which contains the background) will be subjected to re-drawing during resize.

Hence, this gets rid of the flickering - which is the result of many attempts of re-drawing process towards child controls such as panels (contained inside the form) - and speed up the resize process.