Brettb.Com
  HOME | ABOUT ME | BIOTECHNOLOGY | ARTICLES | GALLERY | CONTACT
Search: Go
TECHNICAL ARTICLES
 ASP
 ASP.NET
 JavaScript
 Transact SQL
 Other Articles
 Software Reviews

PHOTO GALLERIES
 Canon EOS 300D Samples
 Akihabara Maids!
 More Galleries...

TRAVEL LOG
 2009: China
 2008: Tokyo
 2007: Tokyo
 2006: Hong Kong
 2005: New York City

MORE STUFF
 Search Engine Optimisation
 Build an ASP Search Engine
 My Tropical Fishtank
 Autoglass
 SQL Month Name
 SQL Get Date Today
 SQL Year Month
 Other New Stuff...

POPULAR STUFF
 Regular Expressions
 Index Server & ASP
 JavaScript Ad Rotator

Home > ASP.NET Articles

How to Determine the Number of Frames in an Animated GIF

This article how to describes how to determine the number of frames in a GIF animation using Microsoft's .NET Framework. It can also be used to detect whether a GIF image is animated. Using ASP.NET the technique could also be applied to determine whether files uploaded to the web server are animated GIFs. The code for this article is available in both C# and VB.NET languages.

C# Code for Determining Animated GIF Framecount

The C# code to determine if an image contains animation frames is shown below:

            //Create an image object from a file on disk
            System.Drawing.Image MyImage = System.Drawing.Image.FromFile(Server.MapPath("
AnimatedGIF.gif"));

            //Create a new FrameDimension object from this image
            System.Drawing.Imaging.FrameDimension FrameDimensions = new System.Drawing.Imaging.FrameDimension(MyImage.FrameDimensionsList[0]);

            //Determine the number of frames in the image
            //Note that all images contain at least 1 frame, but an animated GIF
            //will contain more than 1 frame.

            int NumberOfFrames = MyImage.GetFrameCount(FrameDimensions);

            if (NumberOfFrames > 1)
            {
                Response.Write("Image is an animated GIF with " + NumberOfFrames.ToString() + " frames<br>");
            }
            else
            {
                Response.Write("Image is not an animated GIF.<br>");
            }

Don't forget that other image formats may contain frames, so it would be best to determine that the image is a GIF (e.g. by checking the file extension and/or mime type) before using this code.

Incidentally, images that cannot contain multiple frames such as JPEGs will return a value of 1 as the NumberOfFrames.

VB.NET Code for Image Uploading

'Create an image object from a file on disk
Dim MyImage As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("
AnimatedGIF.gif"))

'Create a new FrameDimension object from this image
Dim FrameDimensions As System.Drawing.Imaging.FrameDimension = New System.Drawing.Imaging.FrameDimension(MyImage.FrameDimensionsList(0))

'Determine the number of frames in the image
'Note that all images contain at least 1 frame, but an animated GIF
'will contain more than 1 frame.

Dim NumberOfFrames As Integer = MyImage.GetFrameCount(FrameDimensions)

If NumberOfFrames > 1 Then
    Response.Write("Image is an animated GIF with " + NumberOfFrames.ToString() + " frames")
Else
    Response.Write("Image is not an animated GIF.")
End If

  Site Map | Privacy Policy

All content is 1995 - 2012