26

I'll keep it short and simple;

is there any way of telling static GIF images apart from animated ones? I'm using C#.

Thanks

pastapockets
  • 1,088
  • 2
  • 13
  • 20

2 Answers2

28

Here's an article about how to determine the number of frames in a GIF animation.

Image i = Image.FromFile(Server.MapPath("AnimatedGIF.gif"));

Imaging.FrameDimension FrameDimensions = 
    new Imaging.FrameDimension(i.FrameDimensionsList[0]);

int frames = i.GetFrameCount(FrameDimensions);

if (frames > 1) 
    Response.Write("Image is an animated GIF with " + frames + " frames");
else 
    Response.Write("Image is not an animated GIF.");

And I assume you could just compare that with 1.

Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153
David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • 2
    +1 Hardcore article in a tiny font! Could you paste a snippet of the code here and link to the article as well? So that if the page or site gets deleted the answer is not lost. – amelvin May 17 '10 at 11:57
  • @amelvin: good idea. i see jeff has already done it, now =) @jeff atwood: props for changing the variable names to convention! – David Hedlund May 17 '10 at 12:43
  • 3
    Thanks for adding the code here, the website is no longer available. – flayn Jun 05 '12 at 10:09
  • 1
    If your objective is to detect animation then answer from deadpixel is the cleaner approach. – Rahatur Dec 05 '17 at 23:26
6

System.Drawing.ImageAnimator.CanAnimate has been available since .NET 1.1.

From MSDN:

Returns a Boolean value indicating whether the specified image contains time-based frames.

Example:

using (Image image = Image.FromFile("somefile.gif"))
{
    if (ImageAnimator.CanAnimate(image))
    {
        // GIF is animated
    }
    else
    {
        // GIF is not animated
    }
}
deadpixel
  • 61
  • 1
  • 4