how to use GifBitmapEncoder in winform?

mc 7,186 Reputation points
2026-05-13T07:20:54.19+00:00

I want to create gif by bitmaps and it seems that I can not use the gifBitmapEncoder

Developer technologies | Windows Forms
0 comments No comments

Answer accepted by question author

Jack Dang (WICLOUD CORPORATION) 18,975 Reputation points Microsoft External Staff Moderator
2026-05-13T08:47:42.5633333+00:00

Hi @mc ,

Thanks for reaching out.

The code snippets below are just for reference, so you may need to adjust them to fit your project structure and requirements.

GifBitmapEncoder belongs to WPF, so it is not available in a plain WinForms project by default. That is why it seems like you cannot use it directly. If you want to use that encoder in a WinForms app, you need to enable WPF support in the project and then convert your System.Drawing.Bitmap objects into WPF image frames before saving.

If your project is SDK-style, add this to the project file:

<PropertyGroup>
  <UseWindowsForms>true</UseWindowsForms>
  <UseWPF>true</UseWPF>
</PropertyGroup>

After that, you can create the GIF like this:

using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Media.Imaging;

public static void SaveGif(IEnumerable<Bitmap> bitmaps, string outputPath)
{
    var encoder = new GifBitmapEncoder();

    foreach (Bitmap bitmap in bitmaps)
    {
        using var ms = new MemoryStream();
        bitmap.Save(ms, ImageFormat.Png);
        ms.Position = 0;

        var frame = BitmapFrame.Create(
            ms,
            BitmapCreateOptions.PreservePixelFormat,
            BitmapCacheOption.OnLoad);

        encoder.Frames.Add(frame);
    }

    using var fs = new FileStream(outputPath, FileMode.Create, FileAccess.Write);
    encoder.Save(fs);
}

You can call it like this:

var frames = new List<Bitmap>
{
    new Bitmap("frame1.png"),
    new Bitmap("frame2.png"),
    new Bitmap("frame3.png")
};

SaveGif(frames, "output.gif");

foreach (var bmp in frames)
{
    bmp.Dispose();
}

If you only need to save a single bitmap as a normal GIF image, the simpler WinForms-friendly option is just:

bitmap.Save("output.gif", ImageFormat.Gif);

One thing to keep in mind is that if you need a real animated GIF with control over frame delays and looping, GifBitmapEncoder can be a bit limited. In that case, a library like Magick.NET or ImageSharp is usually a smoother option for a WinForms app.

Hope this helps! If my answer was helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AI answer

    2026-05-13T07:21:04.9766667+00:00

    GifBitmapEncoder is a WPF imaging class (System.Windows.Media.Imaging in PresentationCore.dll). It is not part of Windows Forms or System.Drawing, so it cannot be used directly in a typical WinForms-only project without adding WPF references and using interop. For a WinForms application, the supported and simpler approach is to use the GDI+ encoders in System.Drawing.

    To create a GIF from bitmaps in WinForms, use System.Drawing.Image/Bitmap and save with the GIF encoder:

    using System.Drawing;
    using System.Drawing.Imaging;
    
    // Example: create an image and save as GIF
    using (var bmp = new Bitmap(200, 200))
    using (var g = Graphics.FromImage(bmp))
    {
        g.Clear(Color.White);
        g.FillEllipse(Brushes.Red, 50, 50, 100, 100);
    
        // Save as GIF using GDI+ encoder
        bmp.Save("output.gif", ImageFormat.Gif);
    }
    

    System.Drawing (managed GDI+) has built-in encoders and decoders for BMP, GIF, JPEG, PNG, and TIFF, which is the recommended way to handle GIFs in WinForms.

    If GifBitmapEncoder must be used, the project needs to reference WPF assemblies (PresentationCore, WindowsBase, etc.) and then use WPF imaging types (BitmapSource, BitmapFrame) instead of System.Drawing.Bitmap, but that is a WPF scenario rather than a WinForms one.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.