Click here to Skip to main content
Click here to Skip to main content

Tagged as

Check the File Format of an Image.

, 15 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

a_pess
Engineer
Egypt Egypt
Oil & Gas Engineer
C# & VB.net
Coding For Fun Only

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 15 May 2014
Article Copyright 2014 by a_pess
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid