Gif4J PRO for Java - GIF ImageIO; Write GIF Images
Gif4J gif animation software logotype
Home Page for Gif4J Software java gif image processing products, solutions and services Products Page for java GIF image processing software Purchase java GIF image managing products Download Gif4J GIF image processing products Java imaging FAQs and Gif4J solutions support Contact Page Customer Area
Gif4J PRO java gif imaging class library logo

Encode and Save a GIF Image

The GifEncoder java class lets programmers encode and save images out to a file or output stream using the GIF file format (GIF89a version) with a single method call. The next methods to encode and save images as GIFs are available (please consult the Gif4J PRO Java API for more info):

    public static final void encode(BufferedImage image, DataOutput dataOutput)
            throws IOException;

    public static final void encode(BufferedImage image, File output)
            throws IOException;

    public static final void encode(BufferedImage image, OutputStream outputStream)
            throws IOException;

    public static final void encode(GifImage gifImage, File output)
            throws IOException;

    public static final void encode(GifImage gifImage, File output,
                                    boolean forceGlobalColorTableUsage)
            throws IOException;

    public static final void encode(GifImage gifImage, OutputStream outputStream)
            throws IOException;

    public static final void encode(GifImage gifImage, OutputStream outputStream,
                                    boolean forceGlobalColorTableUsage)
            throws IOException;

The first three methods can be used to encode BufferedImage(s) directly to non-animated (consists of one frame) gif image files. If the specified BufferedImage contains more than 256 unique colors then it'll be automatically quantized using the Gif4J Java Color Quantizer.

Parameter forceGlobalColorTableUsage forces Global Color Table use. If this parameter is set to true then Local Color Tables from all frames will be union to one Global Color Table. It's useful to optimize final image size (every Local Color Table takes up to 768 bytes). Especially this option is recommended to set for encoding gif images with lots of internal frames (for example, after applying Morphing Filters).

Gif4J PRO Java Example: Save Image as a GIF to a file

import com.gif4j.ImageUtils;
import com.gif4j.GifEncoder;

import java.awt.Image;
import java.awt.image.BufferedImage;

 // ...

     public void saveImageAsGif(Image image, File fileToSavethrows IOException {
         // convert Image to the BufferedImage
         BufferedImage bufferedImage = ImageUtils.toBufferedImage(image);
         // save image
         GifEncoder.encode(bufferedImage, fileToSave);

Gif4J PRO Java Example: Save array of images as an animated GIF to a file

import com.gif4j.GifEncoder;
import com.gif4j.GifFrame;
import com.gif4j.GifImage;

import java.awt.image.BufferedImage;

   // ...

    public void saveImageArrayAsAnimatedGif(BufferedImage[] images, File fileToSave)
            throws IOException {
        // create new GifImage instance
        GifImage gifImage = new GifImage();
        // set default delay between gif frames
        // add comment to gif image
        gifImage.addComment("Animated GIF image example");
        // add images wrapped by GifFrame
        for (int i = 0; i < images.length; i++)
            gifImage.addGifFrame(new GifFrame(images[i]));
        // save animated gif image
        GifEncoder.encode(gifImage, fileToSave);

Write GIF Images with Java ImageIO

You can find more infomation about the Java Image I/O API here.
Just one quotation: "The Java Image I/O API provides a pluggable architecture for working with images stored in files and accessed across the network. It offers substantially more flexibility and power than the previously available APIs for loading and saving images."

To enable ImageIO support by the Gif4J PRO you just need to add library jar file to the CLASSPATH. The Java Image I/O API should automatically resolve the Gif4J library as a plug-in, load the Gif4J service provider classes, and registers the Gif4J service provider instance.

The next code checks that the Gif4J PRO has been resolved correctly:

    Iterator iterator = ImageIO.getImageWritersByFormatName("gif");
    while (iterator.hasNext()) {
       ImageWriter imageWriter = (;
        ImageWriterSpi spi = imageWriter.getOriginatingProvider();
        System.out.println("Vendor: "+spi.getVendorName());
        System.out.println("Format description: "
                + imageWriter.getOriginatingProvider().getDescription(null)+"\n");

The output should contain something like this:

Vendor: Gif4J Software,
Format description: GRAPHICS INTERCHANGE FORMAT(sm) Version 89a

If nothing has been found then you should call ImageIO.scanForPlugins() method to force the ImageIO scans the application class path for plug-ins.
If the Gif4J service provider has been detected and loaded then an image Writing in the gif format is equally simple:

        BufferedImage bi = null;
// skip image initialization
        try {
            File f = new File("c:\\images\\myimage.gif");
            ImageIO.write(bi, "gif", f);
        catch (IOException e) {

Gif4J java gif imaging lib (professional)
Gif4J Java Color Quantizer
Quality Measurements & Benchmark Testing
Test Online
Gif4J PRO Tutorial
Loading GIF Images
Creating GIF Images
Creating GIF Frames
Transform GIF Images
Saving GIF Images
Morphing Filters
Text Rendering
Gif4J java gif imaging lib (light)
Gif4J LIGHT Tutorial
Creating GIF Images
Creating GIF Frames
Saving GIF Images
Home |  Privacy Policy |  Legal Information 
© Gif4J Software 2004-2017