Google Analytics

Search This Blog

Wednesday, April 09, 2014

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

http://msdn.microsoft.com/en-us/magazine/cc163630.aspx#S7

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
        Get
            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, _
                Imaging.PixelFormat.Format32bppPArgb)
                Dim g As Graphics = Graphics.FromImage(Me.RenderBmp)
                g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.High
                g.DrawImage(value, New Rectangle(0, 0, Me.RenderBmp.Width, _
                Me.RenderBmp.Height))
                g.Dispose()
            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.

http://msdn.microsoft.com/en-us/magazine/cc163630.aspx#S7

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.

http://msdn.microsoft.com/en-us/library/3t7htc9c%28v=vs.110%29.aspx

3.) Extended Windows Style

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

http://stackoverflow.com/questions/20585643/improve-performance-when-resizing-a-form-with-a-hi-res-background-image

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
    Get
        Dim cp As CreateParams = MyBase.CreateParams
        cp.ExStyle = cp.ExStyle Or &H2000000
        Return cp
    End Get
End Property


http://msdn.microsoft.com/en-us/library/system.windows.forms.createparams%28v=vs.110%29.aspx

http://social.msdn.microsoft.com/Forums/windows/en-US/aaed00ce-4bc9-424e-8c05-c30213171c2c/flickerfree-painting?forum=winforms

http://social.msdn.microsoft.com/Forums/windows/en-US/f48ca7f5-7647-4f23-a022-0bf993a19dd6/borderless-form-right-click-menu-of-taskbar?forum=winforms

http://social.msdn.microsoft.com/Forums/windows/en-US/2f2c53d6-a3e3-4b18-bf89-65e1a188e1ab/memory-useage-by-readonly-property-createparams-in-windows-application?forum=winformsapplications

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.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600%28v=vs.85%29.aspx

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.

No comments: