Click here to Skip to main content
15,881,757 members
Articles / Multimedia / GDI+
Tip/Trick

Check the File Format of an Image.

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
15 May 2014CPOL 10.4K   2  
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.

C++
''' <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:

C++
''' <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.

C++
''' <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

C++
' 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)


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

Comments and Discussions

 
-- There are no messages in this forum --