All functions succeed, the final file is animating, but not looping.
It stops at the last frame.
How can I tell it to loop indefinitely?
By default, the loop count is 1
. As you already found that it only displays from the first frame to the last one then stops.
In order to make it loop indefinitely you need to set the loop count to 0
which specifies that the animation should be displayed infinitely.
The following is an example of creating an indefinitely loop animated gif in two steps using GDI+ you can refer to.
First, create multiple gif images. Every gif image has one frame. Set loop count for per gif image.
// Get the CLSID of the GIF encoder.
CLSID encoderClsid;
GetEncoderClsid(L"image/gif", &encoderClsid);
Image* singleFrameGif = new Image(fileName);
// Set the loop count.
PropertyItem* propItemLoopCount = new PropertyItem;
SHORT loopCount = 0; //A value of 0 specifies that the animation should be displayed infinitely.
propItemLoopCount->id = PropertyTagLoopCount;
propItemLoopCount->length = sizeof(loopCount);
propItemLoopCount->type = PropertyTagTypeShort;
propItemLoopCount->value = &loopCount;
singleFrameGif->SetPropertyItem(propItemLoopCount);
//Save this image to a file.
singleFrameGif->Save(newName, &encoderClsid, NULL);
Second, add created gif images to one file to make up of a multiple-frames gif image.
EncoderParameters encoderParameters;
ULONG parameterValue;
Status stat;
// An EncoderParameters object has an array of
// EncoderParameter objects. In this case, there is only
// one EncoderParameter object in the array.
encoderParameters.Count = 1;
// Initialize the one EncoderParameter object.
encoderParameters.Parameter[0].Guid = EncoderSaveFlag;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;
encoderParameters.Parameter[0].Value = ¶meterValue;
// Create two image objects.
Image* multi = new Image(L"1.gif");
// Save the first page (frame).
parameterValue = EncoderValueMultiFrame;
stat = multi->Save(L"MultiFrame.gif", &encoderClsid, &encoderParameters);
if (stat == Ok)
printf("Page 1 saved successfully.\n");
Image* page2 = new Image(L"2.gif");
// Save the second page (frame).
parameterValue = EncoderValueFrameDimensionTime;
stat = multi->SaveAdd(page2, &encoderParameters);
if (stat == Ok)
printf("Page 2 saved successfully.\n");
// Close the multiframe file.
parameterValue = EncoderValueFlush;
stat = multi->SaveAdd(&encoderParameters);
if (stat == Ok)
printf("File closed successfully.\n");
More reference: "Creating and Saving a Multiple-Frame Image".