1

I'm using gdi+ to get animated gif loop count, when I test the code with gifs made by myself, I found the count is always 1 less than it actually is as played in IE11 and Chrome,

except I set the loop count as 1, it will be 1, it's right,

if I set the loop count as 2,3, and so on, the code get it as 1,2, and so on.

Although the gifs with loop count 1 and 2 are played correctly in IE11 and Chrome, the code get the same value as 1.

I can't guess if there is anything in gif file expresses it will repeat or not.

After I use command tool gifsicle to list the gif infomation, I found the difference of the loop count 1 and 2 gifs, the loop count 2 gif has a line

loop count 1

while loop count 1 gif has not.

But I don't know what's the code of gdi+ to distinguish the difference.

This is the code:

// new Bitmap
m_pBitmap = new Bitmap(szFilePath);

// get loop count
PropertyItem* pPropLoopCount;
UINT uSize = m_pBitmap->GetPropertyItemSize(PropertyTagLoopCount);
PropertyItem* pPropertyItem = (PropertyItem*)new char[uSize];
Status status = m_pBitmap->GetPropertyItem(PropertyTagLoopCount, uSize, pPropertyItem);
if (Status::Ok != status)
    return false;
m_sLoopCount = *((SHORT*)pPropLoopCount->value);

document of MSDN just said:

For an animated GIF image, the number of times to display the animation. A value of 0 specifies that the animation should be displayed infinitely.

about PropertyTagLoopCount.

After all, two questions:

  1. Is loop count the code got means actual play times minus 1?
  2. What's the code of gdi+ to distinguish actual loop count 1 and 2 gifs?
Z.s
  • 11
  • 2

1 Answers1

0

I ran into the same issue in C# using GetPropertyItem. I believe GetPropertyItem(20737) always returns a value even if it's missing from the file.

This is how the actual byte data is stored:

http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension

How to decode the application extension block of GIF?

As you can see from this comment, the rules are:

  • Value 0 in "NETSCAPE2.0" means loop forever.
  • Missing "NETSCAPE2.0" block means 0 repetitions = loop once.
  • Value 1 in "NETSCAPE2.0" means 1 repetition = 2 loops.
  • Value 2 in "NETSCAPE2.0" means 2 repetitions = 3 loops.

The way I solved it was to search for the NETSCAPE bytes and only use the GetPropertyItem value if it exists.

I've got a gist covering these scenarios in C# if it helps at all:

https://gist.github.com/NeilBostrom/8dde778f06e6f337ad73aea6e0152bbc

Neil Bostrom
  • 2,254
  • 21
  • 24