Convert Image to 32 bit RGB Format in VB.NET

Description: Converts a provided Image into Format32bppRgb pixel format which can applied to indexed gif images etc. Great for situations where you are fetching a graphics object from the image.
Tested Platform: Visual Studio 2012, 32-bit, Windows 7
Language: VB.NET
' Function handles converting images to an 32 bit RGB pixel format

Private Function ConvertImageToRGBFormat(img As Image) As Image
        If Not img.PixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppRgb Then
            Dim temp As Bitmap = New Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
            Dim g As Graphics = Graphics.FromImage(temp)
            g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel)
            g.Dispose()

            Return temp
        End If

        Return img
End Function

Submitted: April 06, 2013

Return to the snippets listing