Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Check the File Format of an Image.

0.00/5 (No votes)
15 May 2014 1  
Image File Format

Introduction

Two years ago or less, I wrote a class to handle the animated "*.gif" files and was in need to check the format type of the image, later on was able to do by checking both FrameCount or RawFormat Guid. The below methods indicates how to do.

First Method:

This method may modified to check other formats like bmp, png...and ..so on.

    ''' <summary>
    ''' Chech if Image is GIf animated image, using imageformat
    ''' </summary>
    ''' <param name="img">image to check</param>
    ''' <returns>Flag to indicate that the image passed to the function is of gif type</returns>
    ''' <remarks>if the image passed to the function is of gif type, the reurn value shall be true</remarks>
    Friend Shared Function IsGifSupported(img As Image) As Boolean
        Return img IsNot Nothing AndAlso img.RawFormat.Guid = ImageFormat.Gif.Guid
    End Function 

Second Method:

    ''' <summary>
    ''' Chech if Image is GIf animated image, using framacount
    ''' </summary>
    ''' <param name="img">image to check</param>
    ''' <returns>Flag to indicate that the image passed to the function is of gif type</returns>
    ''' <remarks>if the image passed to the function is of gif type, the reurn value shall be true</remarks>
    Friend Shared Function IsGifImage(ByVal img As Image) As Boolean
        Return img IsNot Nothing AndAlso ((img.GetFrameCount(New FrameDimension(img.FrameDimensionsList(0)))) > 1)
    End Function 

Third Method:

Finally and by mixing both methods I came with much better alternative.

    ''' <summary>
    ''' Chech if Image is GIf animated image, using both frame count and image format
    ''' </summary>
    ''' <param name="img">image to check</param>
    ''' <returns>Flag to indicate that the image passed to the function is of gif type</returns>
    ''' <remarks>if the image passed to the function is of gif type, the reurn value shall be true</remarks>
    Friend Shared Function IsAnimatedImage(img As Image) As Boolean
        Return img IsNot Nothing AndAlso img.RawFormat.Guid = ImageFormat.Gif.Guid AndAlso ((img.GetFrameCount(New FrameDimension(img.FrameDimensionsList(0)))) > 1)
    End Function 

Using the code

in the paint method, add the code as below or use it at your convenient

        ' Define an image
        Dim bmp As Image = My.Resources.calypso_jpg
        ' Apply the function to check image format
        If Not IsAnimatedImage(bmp) Then
            ' draw the image if it is true or not
            e.Graphics.DrawImage(bmp, 0, 0)
        End If 

Enjoy It, comments are welcomed to produce better alternative

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here