Recursive AI

Extract animated GIF frames as PNG in C#

OneImage is a free, open-source image library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Call ToImage() on each frame. dots.gif in the zip shows a blue dot moving above a line.

Terminal
dotnet add package RecursiveAI.OneImage

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneImage;
 
var gif = Image.Load("dots.gif");
foreach (var frame in gif.Frames)
    frame.ToImage()
        .Save($"frame-{frame.Index:D4}.png");

Unzip the example, then dotnet run.

Writes frame-0000.png through frame-0002.png without printing anything.

The full program

The download also prints each filename after saving it.

C#
using OneImage;
 
var gif = Image.Load("dots.gif");
foreach (var frame in gif.Frames)
{
    var file = $"frame-{frame.Index:D4}.png";
    frame.ToImage().Save(file);
    Console.WriteLine(file);
}
Text
frame-0000.png
frame-0001.png
frame-0002.png

All three PNGs are 64×24. If decoding throws ImageFormatException partway through a damaged GIF, earlier PNGs may already be on disk.

Variations

Export only the first frame. Replace the loop with this line to make a still preview from the sample.

C#
gif.Frames[0].ToImage().Save("preview.png");

This writes preview.png, a 64×24 image with the dot at the left.

Record the frame delays. Replace Console.WriteLine(file); in the full program with this line if you need the timing as well as the image files.

C#
Console.WriteLine(
    $"{file}: {frame.Delay.TotalMilliseconds} ms");
Text
frame-0000.png: 100 ms
frame-0001.png: 200 ms
frame-0002.png: 300 ms

The short answer

Load the GIF, loop over gif.Frames in order, and call frame.ToImage().Save(...) with a different .png path for each frame. ToImage() takes just that one frame out of the animation. Use frame.Index to name the files from zero and frame.Delay if you also need to know how long each one was shown.

How do you extract every frame from an animated GIF?

Read the Frames collection rather than asking the whole Image for its pixels. The OneImage guide notes that pixel readers such as ToArray on the image itself “use frame 0 only and ignore the rest, without warning.” That’s useful for a still preview, but it won’t export an animation. Use a different name for each frame so the next save doesn’t replace the previous file. Leading zeroes keep the names in playback order when sorted as text, up through frame-9999.png with the example’s naming pattern.

A GIF can store only the rectangle that changed and say whether to keep or clear the previous picture. OneImage exposes animations “as fully composited frames of the canvas size (disposal and blending already applied).” For example, the final two frames in dots.gif store only changes around the dot, but both PNGs still include the whole line beneath it. You don’t have to reconstruct the frames from rectangles yourself.

Can you convert an animated GIF to PNG and keep the animation?

This recipe writes separate still PNGs, not an animated PNG. Each PNG contains the finished picture for that moment, not the delay before the next frame or the GIF’s loop count. If you’re taking screenshots for a storyboard, you can skip the timing. If you plan to rebuild the animation later, record Delay for every frame and the source image’s LoopCount before you discard the GIF. If you only need to resize while keeping a GIF animated, resize the whole image and save it as a GIF instead of extracting it.

All oneimage recipes