• Articles
  • API Documentation
Search Results for

    Show / Hide Table of Contents
    • ImageSharp
      • Getting Started
        • Pixel Formats
        • Image Formats
        • Processing Images
          • Resizing Images
          • Create an animated GIF
        • Working with Pixel Buffers
        • Configuration
        • Memory Management
        • Security Considerations
    • ImageSharp.Drawing
      • Getting Started
    • ImageSharp.Web
      • Getting Started
        • Processing Commands
        • Image Providers
        • Image Caches
    • Fonts
      • Getting Started
      • Custom Rendering

    Create an animated GIF

    The following example demonstrates how to create a animated gif from several single images. The different image frames will be images with different colors.

    // Image dimensions of the gif.
    const int width = 100;
    const int height = 100;
    
    // Delay between frames in (1/100) of a second.
    const int frameDelay = 100;
    
    // For demonstration: use images with different colors.
    Color[] colors = {
        Color.Green,
        Color.Red
    };
    
    // Create empty image.
    using Image<Rgba32> gif = new(width, height, Color.Blue);
    
    // Set animation loop repeat count to 5.
    var gifMetaData = gif.Metadata.GetGifMetadata();
    gifMetaData.RepeatCount = 5;
    
    // Set the delay until the next image is displayed.
    GifFrameMetadata metadata = gif.Frames.RootFrame.Metadata.GetGifMetadata();
    metadata.FrameDelay = frameDelay;
    for (int i = 0; i < colors.Length; i++)
    {
        // Create a color image, which will be added to the gif.
        using Image<Rgba32> image = new(width, height, colors[i]);
    
        // Set the delay until the next image is displayed.
        metadata = image.Frames.RootFrame.Metadata.GetGifMetadata();
        metadata.FrameDelay = frameDelay;
    
        // Add the color image to the gif.
        gif.Frames.AddFrame(image.Frames.RootFrame);
    }
    
    // Save the final result.
    gif.SaveAsGif("output.gif");
    
    • Edit this page
    In this article
    Back to top Generated by DocFX