Check the file format of an Image.

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.
    ''' 
    ''' Chech if Image is GIf animated image, using imageformat
    ''' 
    ''' image to check
    ''' Flag to indicate that the image passed to the function is of gif type
    ''' if the image passed to the function is of gif type, the reurn value shall be true
    Friend Shared Function IsGifSupported(img As Image) As Boolean
        Return img IsNot Nothing AndAlso img.RawFormat.Guid = ImageFormat.Gif.Guid
    End Function 

Second Method:

    ''' 
    ''' Chech if Image is GIf animated image, using framacount
    ''' 
    ''' image to check
    ''' Flag to indicate that the image passed to the function is of gif type
    ''' if the image passed to the function is of gif type, the reurn value shall be true
    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.
    ''' 
    ''' Chech if Image is GIf animated image, using bith frame count and image format
    ''' 
    ''' image to check
    ''' Flag to indicate that the image passed to the function is of gif type
    ''' if the image passed to the function is of gif type, the reurn value shall be true
    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, and comments are welcomed to produce better alternative

Comments

Popular posts from this blog

مقدمة الي تشفير الحروف الأبجدية العربية

VB.NET Translucent Control using GDI+

Add Custom Event to a Class in VB.NET