Convert Image to 32 bit RGB Format

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. Requires you to import System.Drawing
Tested Platform: .NET 4.8, Visual Studio 2022, Windows 10
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

Posted: March 20, 2023

Return to the snippets listing