Class GifOptions

Class GifOptions

Namespace: Aspose.Imaging.ImageOptions
Assembly: Aspose.Imaging.dll (25.2.0)

The API for Graphical Interchange Format (GIF) raster image file creation offers developers comprehensive options for generating GIF images with precise control. With features to set background color, color palette, resolution, interlaced type, transparent color, XMP metadata container, and image compression, this API ensures flexibility and efficiency in creating optimized and visually appealing GIFs tailored to specific application requirements.

[JsonObject(MemberSerialization.OptIn)]
public class GifOptions : ImageOptionsBase, IDisposable, IHasXmpData, IHasMetadata, ICloneable

Inheritance

objectDisposableObjectImageOptionsBaseGifOptions

Implements

IDisposable, IHasXmpData, IHasMetadata, ICloneable

Inherited Members

ImageOptionsBase.Clone(), ImageOptionsBase.ReleaseManagedResources(), ImageOptionsBase.KeepMetadata, ImageOptionsBase.XmpData, ImageOptionsBase.Source, ImageOptionsBase.Palette, ImageOptionsBase.ResolutionSettings, ImageOptionsBase.VectorRasterizationOptions, ImageOptionsBase.BufferSizeHint, ImageOptionsBase.MultiPageOptions, ImageOptionsBase.FullFrame, ImageOptionsBase.ProgressEventHandler, DisposableObject.Dispose(), DisposableObject.ReleaseManagedResources(), DisposableObject.ReleaseUnmanagedResources(), DisposableObject.VerifyNotDisposed(), DisposableObject.Disposed, object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Examples

This example demonstrates the use of different classes from SaveOptions Namespace for export purposes. An image of type Gif is loaded into an instance of Image and then exported out to several formats.```csharp [C#]

                                                                                                                                                                                                                string dir = "c:\\temp\\";

                                                                                                                                                                                                                //Load an existing image (of type Gif) in an instance of Image class
                                                                                                                                                                                                                using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.gif"))
                                                                                                                                                                                                                {
                                                                                                                                                                                                                    //Export to BMP file format using the default options
                                                                                                                                                                                                                    image.Save(dir + "output.bmp", new Aspose.Imaging.ImageOptions.BmpOptions());

                                                                                                                                                                                                                    //Export to JPEG file format using the default options
                                                                                                                                                                                                                    image.Save(dir + "output.jpg", new Aspose.Imaging.ImageOptions.JpegOptions());

                                                                                                                                                                                                                    //Export to PNG file format using the default options
                                                                                                                                                                                                                    image.Save(dir + "output.png", new Aspose.Imaging.ImageOptions.PngOptions());

                                                                                                                                                                                                                    //Export to TIFF file format using the default options
                                                                                                                                                                                                                    image.Save(dir + "output.tif", new Aspose.Imaging.ImageOptions.TiffOptions(Aspose.Imaging.FileFormats.Tiff.Enums.TiffExpectedFormat.Default));
                                                                                                                                                                                                                }

The following example shows how to convert a multipage vector image to GIF format in general way without referencing to a particular image type.```csharp
[C#]

                                                                                                                                                           string dir = "C:\\aspose.imaging\\net\\misc\\ImagingReleaseQATester\\Tests\\testdata\\2548";
                                                                                                                                                           string inputFilePath = System.IO.Path.Combine(dir, "Multipage.cdr");
                                                                                                                                                           string outputFilePath = System.IO.Path.Combine(dir, "Multipage.cdr.gif");

                                                                                                                                                           Aspose.Imaging.ImageOptionsBase exportOptions = new Aspose.Imaging.ImageOptions.GifOptions();

                                                                                                                                                           using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(inputFilePath))
                                                                                                                                                           {
                                                                                                                                                               exportOptions.MultiPageOptions = null;

                                                                                                                                                               // Export only first two pages. These pages will be presented as animated frames in the output GIF.
                                                                                                                                                               Aspose.Imaging.IMultipageImage multipageImage = image as Aspose.Imaging.IMultipageImage;
                                                                                                                                                               if (multipageImage != null && (multipageImage.Pages != null && multipageImage.PageCount > 2))
                                                                                                                                                               {
                                                                                                                                                                   exportOptions.MultiPageOptions = new Aspose.Imaging.ImageOptions.MultiPageOptions(new Aspose.Imaging.IntRange(0, 2));
                                                                                                                                                               }

                                                                                                                                                               if (image is Aspose.Imaging.VectorImage)
                                                                                                                                                               {
                                                                                                                                                                   exportOptions.VectorRasterizationOptions = (Aspose.Imaging.ImageOptions.VectorRasterizationOptions)image.GetDefaultOptions(new object[] { Aspose.Imaging.Color.White, image.Width, image.Height });
                                                                                                                                                                   exportOptions.VectorRasterizationOptions.TextRenderingHint = Aspose.Imaging.TextRenderingHint.SingleBitPerPixel;
                                                                                                                                                                   exportOptions.VectorRasterizationOptions.SmoothingMode = Aspose.Imaging.SmoothingMode.None;
                                                                                                                                                               }

                                                                                                                                                               image.Save(outputFilePath, exportOptions);
                                                                                                                                                           }

This example shows how to Loads Pixel information in an Array of Type Color, manipulates the array and set it back to the image. To perform these operations, this example creates a new Image file (in GIF format) uisng MemoryStream object.```csharp [C#]

                                                                                                                                                                                                                                                     //Create an instance of MemoryStream
                                                                                                                                                                                                                                                     using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                         //Create an instance of GifOptions and set its various properties including the Source property
                                                                                                                                                                                                                                                         Aspose.Imaging.ImageOptions.GifOptions gifOptions = new Aspose.Imaging.ImageOptions.GifOptions();
                                                                                                                                                                                                                                                         gifOptions.Source = new Aspose.Imaging.Sources.StreamSource(stream);

                                                                                                                                                                                                                                                         //Create an instance of Image
                                                                                                                                                                                                                                                         using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Create(gifOptions, 500, 500))
                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                             //Get the pixels of image by specifying the area as image boundary
                                                                                                                                                                                                                                                             Aspose.Imaging.Color[] pixels = image.LoadPixels(image.Bounds);

                                                                                                                                                                                                                                                             //Loop over the Array and sets color of alrenative indexed pixel
                                                                                                                                                                                                                                                             for (int index = 0; index < pixels.Length; index++)
                                                                                                                                                                                                                                                             {
                                                                                                                                                                                                                                                                 if (index % 2 == 0)
                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                                                                                                     //Set the indexed pixel color to yellow
                                                                                                                                                                                                                                                                     pixels[index] = Aspose.Imaging.Color.Yellow;
                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                 else
                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                                                                                                     //Set the indexed pixel color to blue
                                                                                                                                                                                                                                                                     pixels[index] = Aspose.Imaging.Color.Blue;
                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                             }

                                                                                                                                                                                                                                                             //Apply the pixel changes to the image
                                                                                                                                                                                                                                                             image.SavePixels(image.Bounds, pixels);

                                                                                                                                                                                                                                                             // save all changes.
                                                                                                                                                                                                                                                             image.Save();
                                                                                                                                                                                                                                                         }

                                                                                                                                                                                                                                                         // Write MemoryStream to File
                                                                                                                                                                                                                                                         using (System.IO.FileStream fileStream = new System.IO.FileStream(@"C:\temp\output.gif", System.IO.FileMode.Create))
                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                             stream.WriteTo(fileStream);
                                                                                                                                                                                                                                                         }   
                                                                                                                                                                                                                                                     }

## Constructors

### <a id="Aspose_Imaging_ImageOptions_GifOptions__ctor"></a> GifOptions\(\)

Initializes a new instance of the Aspose.Imaging.ImageOptions.GifOptions class.

```csharp
[JsonConstructor]
public GifOptions()

GifOptions(GifOptions)

Initializes a new instance of the Aspose.Imaging.ImageOptions.GifOptions class.

public GifOptions(GifOptions gifOptions)

Parameters

gifOptions GifOptions

The GIF Options.

Properties

BackgroundColor

Gets or sets the background color.

[JsonProperty]
public Color BackgroundColor { get; set; }

Property Value

Color

BackgroundColorIndex

Gets or sets the GIF background color index.

public byte BackgroundColorIndex { get; set; }

Property Value

byte

ColorResolution

Gets or sets the GIF color resolution.

public byte ColorResolution { get; set; }

Property Value

byte

Examples

This example shows how to save a BMP image to GIF format using various options.```csharp [C#]

                                                                                      string dir = "c:\\temp\\";

                                                                                      using (Aspose.Imaging.Image bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(1000, 1000))
                                                                                      {
                                                                                          // Fill the entire image with the blue-yellow gradient.
                                                                                          Aspose.Imaging.Brushes.LinearGradientBrush gradientBrush = new Aspose.Imaging.Brushes.LinearGradientBrush(
                                                                                                  new Aspose.Imaging.Point(0, 0),
                                                                                                  new Aspose.Imaging.Point(bmpImage.Width, bmpImage.Height),
                                                                                                  Aspose.Imaging.Color.Blue,
                                                                                                  Aspose.Imaging.Color.Yellow);

                                                                                          Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(bmpImage);
                                                                                          graphics.FillRectangle(gradientBrush, bmpImage.Bounds);

                                                                                          Aspose.Imaging.ImageOptions.GifOptions saveOptions = new Aspose.Imaging.ImageOptions.GifOptions();

                                                                                          // The number of bits required to store a color, minus 1.
                                                                                          saveOptions.ColorResolution = 7;

                                                                                          // Palette correction means that whenever image is exported to GIF the source image colors will be analyzed
                                                                                          // in order to build the best matching palette (in case image Palette does not exist or not specified in the options)
                                                                                          saveOptions.DoPaletteCorrection = true;

                                                                                          // Load a GIF image in a progressive way.
                                                                                          // An interlaced GIF doesn't display its scanlines linearly from top to bottom, but instead reorders it
                                                                                          // so the content of the GIF becomes clear even before it finishes loading.
                                                                                          saveOptions.Interlaced = true;

                                                                                          // Save as a lossless GIF.
                                                                                          using (System.IO.Stream stream = System.IO.File.OpenWrite(dir + "output.gif"))
                                                                                          {
                                                                                              bmpImage.Save(stream, saveOptions);
                                                                                              System.Console.WriteLine("The size of the lossless GIF: {0} bytes.", stream.Length);
                                                                                          }

                                                                                          // Set the maximum allowed pixel difference. If greater than zero, lossy compression will be used.
                                                                                          // The recommended value for optimal lossy compression is 80. 30 is very light compression, 200 is heavy.
                                                                                          saveOptions.MaxDiff = 80;

                                                                                          // Save as a lossy GIF.
                                                                                          using (System.IO.Stream stream = System.IO.File.OpenWrite(dir + "output.lossy.gif"))
                                                                                          {
                                                                                              bmpImage.Save(stream, saveOptions);
                                                                                              System.Console.WriteLine("The size of the lossy GIF: {0} bytes.", stream.Length);
                                                                                          }
                                                                                      }

                                                                                      //The output may look like this:
                                                                                      //The size of the lossless GIF: 212816 bytes.
                                                                                      //The size of the lossy GIF: 89726 bytes.

#### Remarks

Color Resolution - Number of bits per primary color available
to the original image, minus 1. This value represents the size of
the entire palette from which the colors in the graphic were
selected, not the number of colors actually used in the graphic.
For example, if the value in this field is 3, then the palette of
the original image had 4 bits per primary color available to create
the image.  This value should be set to indicate the richness of
the original palette, even if not every color from the whole
palette is available on the source machine.

### <a id="Aspose_Imaging_ImageOptions_GifOptions_DoPaletteCorrection"></a> DoPaletteCorrection

Gets or sets a value indicating whether palette correction is applied.

```csharp
public bool DoPaletteCorrection { get; set; }

Property Value

bool

Examples

This example shows how to save a BMP image to GIF format using various options.```csharp [C#]

                                                                                      string dir = "c:\\temp\\";

                                                                                      using (Aspose.Imaging.Image bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(1000, 1000))
                                                                                      {
                                                                                          // Fill the entire image with the blue-yellow gradient.
                                                                                          Aspose.Imaging.Brushes.LinearGradientBrush gradientBrush = new Aspose.Imaging.Brushes.LinearGradientBrush(
                                                                                                  new Aspose.Imaging.Point(0, 0),
                                                                                                  new Aspose.Imaging.Point(bmpImage.Width, bmpImage.Height),
                                                                                                  Aspose.Imaging.Color.Blue,
                                                                                                  Aspose.Imaging.Color.Yellow);

                                                                                          Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(bmpImage);
                                                                                          graphics.FillRectangle(gradientBrush, bmpImage.Bounds);

                                                                                          Aspose.Imaging.ImageOptions.GifOptions saveOptions = new Aspose.Imaging.ImageOptions.GifOptions();

                                                                                          // The number of bits required to store a color, minus 1.
                                                                                          saveOptions.ColorResolution = 7;

                                                                                          // Palette correction means that whenever image is exported to GIF the source image colors will be analyzed
                                                                                          // in order to build the best matching palette (in case image Palette does not exist or not specified in the options)
                                                                                          saveOptions.DoPaletteCorrection = true;

                                                                                          // Load a GIF image in a progressive way.
                                                                                          // An interlaced GIF doesn't display its scanlines linearly from top to bottom, but instead reorders it
                                                                                          // so the content of the GIF becomes clear even before it finishes loading.
                                                                                          saveOptions.Interlaced = true;

                                                                                          // Save as a lossless GIF.
                                                                                          using (System.IO.Stream stream = System.IO.File.OpenWrite(dir + "output.gif"))
                                                                                          {
                                                                                              bmpImage.Save(stream, saveOptions);
                                                                                              System.Console.WriteLine("The size of the lossless GIF: {0} bytes.", stream.Length);
                                                                                          }

                                                                                          // Set the maximum allowed pixel difference. If greater than zero, lossy compression will be used.
                                                                                          // The recommended value for optimal lossy compression is 80. 30 is very light compression, 200 is heavy.
                                                                                          saveOptions.MaxDiff = 80;

                                                                                          // Save as a lossy GIF.
                                                                                          using (System.IO.Stream stream = System.IO.File.OpenWrite(dir + "output.lossy.gif"))
                                                                                          {
                                                                                              bmpImage.Save(stream, saveOptions);
                                                                                              System.Console.WriteLine("The size of the lossy GIF: {0} bytes.", stream.Length);
                                                                                          }
                                                                                      }

                                                                                      //The output may look like this:
                                                                                      //The size of the lossless GIF: 212816 bytes.
                                                                                      //The size of the lossy GIF: 89726 bytes.

#### Remarks

Palette correction means that whenever image is exported to GIF the source image colors will be analyzed
            in order to build the best matching palette (in case image Palette does not exist or not specified in the options).
            The analyze process takes some time however the output image will have the best matching color palette and result is visually better.

### <a id="Aspose_Imaging_ImageOptions_GifOptions_HasTrailer"></a> HasTrailer

Gets or sets a value indicating whether GIF has trailer.

```csharp
public bool HasTrailer { get; set; }

Property Value

bool

HasTransparentColor

Gets or sets a value indicating whether a GIF image has transparent color. If the return value is null, this property is overridden by the source image context.

[JsonProperty]
public bool? HasTransparentColor { get; set; }

Property Value

bool?

Interlaced

True if image should be interlaced.

public bool Interlaced { get; set; }

Property Value

bool

Examples

This example shows how to save a BMP image to GIF format using various options.```csharp [C#]

                                                                                      string dir = "c:\\temp\\";

                                                                                      using (Aspose.Imaging.Image bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(1000, 1000))
                                                                                      {
                                                                                          // Fill the entire image with the blue-yellow gradient.
                                                                                          Aspose.Imaging.Brushes.LinearGradientBrush gradientBrush = new Aspose.Imaging.Brushes.LinearGradientBrush(
                                                                                                  new Aspose.Imaging.Point(0, 0),
                                                                                                  new Aspose.Imaging.Point(bmpImage.Width, bmpImage.Height),
                                                                                                  Aspose.Imaging.Color.Blue,
                                                                                                  Aspose.Imaging.Color.Yellow);

                                                                                          Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(bmpImage);
                                                                                          graphics.FillRectangle(gradientBrush, bmpImage.Bounds);

                                                                                          Aspose.Imaging.ImageOptions.GifOptions saveOptions = new Aspose.Imaging.ImageOptions.GifOptions();

                                                                                          // The number of bits required to store a color, minus 1.
                                                                                          saveOptions.ColorResolution = 7;

                                                                                          // Palette correction means that whenever image is exported to GIF the source image colors will be analyzed
                                                                                          // in order to build the best matching palette (in case image Palette does not exist or not specified in the options)
                                                                                          saveOptions.DoPaletteCorrection = true;

                                                                                          // Load a GIF image in a progressive way.
                                                                                          // An interlaced GIF doesn't display its scanlines linearly from top to bottom, but instead reorders it
                                                                                          // so the content of the GIF becomes clear even before it finishes loading.
                                                                                          saveOptions.Interlaced = true;

                                                                                          // Save as a lossless GIF.
                                                                                          using (System.IO.Stream stream = System.IO.File.OpenWrite(dir + "output.gif"))
                                                                                          {
                                                                                              bmpImage.Save(stream, saveOptions);
                                                                                              System.Console.WriteLine("The size of the lossless GIF: {0} bytes.", stream.Length);
                                                                                          }

                                                                                          // Set the maximum allowed pixel difference. If greater than zero, lossy compression will be used.
                                                                                          // The recommended value for optimal lossy compression is 80. 30 is very light compression, 200 is heavy.
                                                                                          saveOptions.MaxDiff = 80;

                                                                                          // Save as a lossy GIF.
                                                                                          using (System.IO.Stream stream = System.IO.File.OpenWrite(dir + "output.lossy.gif"))
                                                                                          {
                                                                                              bmpImage.Save(stream, saveOptions);
                                                                                              System.Console.WriteLine("The size of the lossy GIF: {0} bytes.", stream.Length);
                                                                                          }
                                                                                      }

                                                                                      //The output may look like this:
                                                                                      //The size of the lossless GIF: 212816 bytes.
                                                                                      //The size of the lossy GIF: 89726 bytes.

### <a id="Aspose_Imaging_ImageOptions_GifOptions_IsPaletteSorted"></a> IsPaletteSorted

Gets or sets a value indicating whether palette entries are sorted.

```csharp
public bool IsPaletteSorted { get; set; }

Property Value

bool

LoopsCount

Gets or sets the loops count (Default 1 loop)

public int LoopsCount { get; set; }

Property Value

int

MaxDiff

Gets or sets the maximum allowed pixel difference. If greater than zero, lossy compression will be used. Recommended value for optimal lossy compression is 80. 30 is very light compression, 200 is heavy. It works best when only little loss is introduced, and due to limitation of the compression algorithm very high loss levels won’t give as much gain. The range of allowed values is [0, 1000].

public int MaxDiff { get; set; }

Property Value

int

Examples

This example shows how to save a BMP image to GIF format using various options.```csharp [C#]

                                                                                      string dir = "c:\\temp\\";

                                                                                      using (Aspose.Imaging.Image bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(1000, 1000))
                                                                                      {
                                                                                          // Fill the entire image with the blue-yellow gradient.
                                                                                          Aspose.Imaging.Brushes.LinearGradientBrush gradientBrush = new Aspose.Imaging.Brushes.LinearGradientBrush(
                                                                                                  new Aspose.Imaging.Point(0, 0),
                                                                                                  new Aspose.Imaging.Point(bmpImage.Width, bmpImage.Height),
                                                                                                  Aspose.Imaging.Color.Blue,
                                                                                                  Aspose.Imaging.Color.Yellow);

                                                                                          Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(bmpImage);
                                                                                          graphics.FillRectangle(gradientBrush, bmpImage.Bounds);

                                                                                          Aspose.Imaging.ImageOptions.GifOptions saveOptions = new Aspose.Imaging.ImageOptions.GifOptions();

                                                                                          // The number of bits required to store a color, minus 1.
                                                                                          saveOptions.ColorResolution = 7;

                                                                                          // Palette correction means that whenever image is exported to GIF the source image colors will be analyzed
                                                                                          // in order to build the best matching palette (in case image Palette does not exist or not specified in the options)
                                                                                          saveOptions.DoPaletteCorrection = true;

                                                                                          // Load a GIF image in a progressive way.
                                                                                          // An interlaced GIF doesn't display its scanlines linearly from top to bottom, but instead reorders it
                                                                                          // so the content of the GIF becomes clear even before it finishes loading.
                                                                                          saveOptions.Interlaced = true;

                                                                                          // Save as a lossless GIF.
                                                                                          using (System.IO.Stream stream = System.IO.File.OpenWrite(dir + "output.gif"))
                                                                                          {
                                                                                              bmpImage.Save(stream, saveOptions);
                                                                                              System.Console.WriteLine("The size of the lossless GIF: {0} bytes.", stream.Length);
                                                                                          }

                                                                                          // Set the maximum allowed pixel difference. If greater than zero, lossy compression will be used.
                                                                                          // The recommended value for optimal lossy compression is 80. 30 is very light compression, 200 is heavy.
                                                                                          saveOptions.MaxDiff = 80;

                                                                                          // Save as a lossy GIF.
                                                                                          using (System.IO.Stream stream = System.IO.File.OpenWrite(dir + "output.lossy.gif"))
                                                                                          {
                                                                                              bmpImage.Save(stream, saveOptions);
                                                                                              System.Console.WriteLine("The size of the lossy GIF: {0} bytes.", stream.Length);
                                                                                          }
                                                                                      }

                                                                                      //The output may look like this:
                                                                                      //The size of the lossless GIF: 212816 bytes.
                                                                                      //The size of the lossy GIF: 89726 bytes.

### <a id="Aspose_Imaging_ImageOptions_GifOptions_PixelAspectRatio"></a> PixelAspectRatio

Gets or sets the GIF pixel aspect ratio.

```csharp
public byte PixelAspectRatio { get; set; }

Property Value

byte

Remarks

Pixel Aspect Ratio - Factor used to compute an approximation of the aspect ratio of the pixel in the original image. If the value of the field is not 0, this approximation of the aspect ratio is computed based on the formula: Aspect Ratio = (Pixel Aspect Ratio + 15) / 64 The Pixel Aspect Ratio is defined to be the quotient of the pixel’s width over its height. The value range in this field allows specification of the widest pixel of 4:1 to the tallest pixel of 1:4 in increments of 1/64th. Values : 0 - No aspect ratio information is given. 1..255 - Value used in the computation.