Click or drag to resize

GifEncoder Class

An ImageEncoder that will write Compuserve Graphics Interchange (GIF) images to a Stream.

Inheritance Hierarchy
SystemObject
  Atalasoft.Imaging.CodecImageEncoder
    Atalasoft.Imaging.CodecGifEncoder

Namespace:  Atalasoft.Imaging.Codec
Assembly:  Atalasoft.dotImage (in Atalasoft.dotImage.dll) Version: 11.4.0.9.0.377 (.NET 4.5.2, x86)
Syntax
public class GifEncoder : ImageEncoder

The GifEncoder type exposes the following members.

Constructors
  NameDescription
Public methodGifEncoder
Initializes a new instance of a %GifEncoder%.
Public methodGifEncoder(Boolean)
Initializes a new instance of a %BmpEncoder% specifying the interlace setting.
Public methodGifEncoder(Boolean, Boolean)
Creates a new instance of GifEncoder specifying whether to interlace the image and optimize color depth.
Top
Properties
  NameDescription
Public propertyBackgroundIndex
Gets or sets the background index to store in the GIF file.
Public propertyInterlace
Gets or sets a value indicating if the image is saved with a pixel row interlacing pattern. Default false
Public propertyOptimizeColorDepth
Gets or sets a value indicating if the bitdepth is optimized based on the number of colors in the palette. Default true.
Public propertySupportedPixelFormats
Returns an array of pixel formats supported by this encoder.
(Overrides ImageEncoderSupportedPixelFormats.)
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodIsPixelFormatSupported
Returns true if the given PixelFormat can be encoded with the derived encoder.
(Inherited from ImageEncoder.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodSave(Stream, AtalaImage, ProgressEventHandler)
Encode an AtalaImage as a Gif image to a stream.
(Overrides ImageEncoderSave(Stream, AtalaImage, ProgressEventHandler).)
Public methodSave(Stream, GifFrameCollection, ProgressEventHandler)
Saves an animated GIF image.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Remarks

This ImageEncoder can be passed into the the AtalaImage or Workspace objects when saving to specify a GIF image.

Gif Images typicaly have one color which is transparent. When saving with a GifFrameCollection object, this is indicated by the TransparentIndex property of each GifFrame.

When saving with a regular AtalaImage or ImageCollection, the transparent index is determined automaticaly. Each color in the palette can have an alpha value between 0 and 255. The GifEncoder will chose (as the transparent color) the first fully transparent color in the palette, that is the first color with an alpha value of 255.

Examples
SaveAnimatedGif (C#)
private void CreateAnimatedGif(string fileName, ImageCollection images)
{
    // Find the maximum size of the animation.
    int width = 0;
    int height = 0;
    foreach(AtalaImage img in images)
    {
        if (img.Width > width)
            width = img.Width;

        if (img.Height > height)
            height = img.Height;
    }

    // Create the frame collection.
    GifFrameCollection col = new GifFrameCollection();
    col.Width = width;
    col.Height = height;
    col.LoopCount = 0;  // infinite
    col.TransparentIndex = -1;  // no transparency

    // Center each image.
    foreach(AtalaImage img in images)
    {
        GifFrame frame = new GifFrame(img, new Point((width - img.Width) / 2, (height - img.Height) / 2));
        col.Add(frame);
    }

    // Save the image.
    FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
    GifEncoder encoder = new GifEncoder();
    encoder.Save(fs, col, null);
    fs.Close();

    // Free the collection.
    col.Dispose();
}
SaveAnimatedGifVB (Visual Basic)
Private  Sub CreateAnimatedGif(ByVal fileName As String, ByVal images As ImageCollection)
    ' Find the maximum size of the animation.
    Dim width As Integer =  0 
    Dim height As Integer =  0 
    Dim img As AtalaImage
    For Each img In images
        If img.Width > width Then
            width = img.Width
        End If

        If img.Height > height Then
            height = img.Height
        End If
    Next

    ' Create the frame collection.
    Dim col As GifFrameCollection =  New GifFrameCollection() 
    col.Width = width
    col.Height = height
    col.LoopCount = 0  ' infinite
    col.TransparentIndex = -1  ' no transparency

    ' Center each image.
    Dim img As AtalaImage
    For Each img In images
        Dim frame As GifFrame =  New GifFrame(img, New Point((width - img.Width) / 2,(height - img.Height) / 2)) 
        col.Add(frame)
    Next

    ' Save the image.
    Dim fs As FileStream =  New FileStream(fileName,FileMode.Create,FileAccess.Write) 
    Dim encoder As GifEncoder =  New GifEncoder() 
    encoder.Save(fs, col, Nothing)
    fs.Close()

    ' Free the collection.
    col.Dispose()
End Sub
See Also