---
title: "Extract animated GIF frames as PNG in C#"
url: https://recursiveai.org/oneimage/cookbook/extract-gif-frames-to-png/
description: "Extract every frame of an animated GIF as a separate PNG in C#. Save full-size composited images in order, with runnable code and a sample GIF."
version: "1.11.2"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneImage) · [GitHub](https://github.com/recursive-ai-oss/oneimage-csharp) · [Docs](https://recursiveai.org/oneimage/docs/) · [Download example (.zip)](https://recursiveai.org/oneimage/cookbook/extract-gif-frames-to-png/extract-gif-frames-to-png.zip)

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

```shell
dotnet add package RecursiveAI.OneImage
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/oneimage/cookbook/extract-gif-frames-to-png/extract-gif-frames-to-png.zip), 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.

```csharp
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.

```csharp
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.

```csharp
Console.WriteLine(
    $"{file}: {frame.Delay.TotalMilliseconds} ms");
```

```text
frame-0000.png: 100 ms
frame-0001.png: 200 ms
frame-0002.png: 300 ms
```

## Related recipes

- [Resize an animated GIF](https://recursiveai.org/oneimage/cookbook/resize-animated-gif/)
- [Split a multipage TIFF](https://recursiveai.org/oneimage/cookbook/split-multipage-tiff/)

## 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](https://recursiveai.org/oneimage/docs/guide/#animation-and-multi-page-files) 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](https://recursiveai.org/oneimage/cookbook/resize-animated-gif/) and save it as a GIF instead of extracting it.
