Class GifOptions
Namespace: Aspose.Imaging.ImageOptions
Assembly: Aspose.Imaging.dll (25.7.0)
The API for Graphical Interchange Format (GIF) raster image file creation offersdevelopers comprehensive options for generating GIF images with precisecontrol. With features to set background color, color palette, resolution,interlaced type, transparent color, XMP metadata container, and imagecompression, this API ensures flexibility and efficiency in creating optimizedand visually appealing GIFs tailored to specific application requirements.
public class GifOptions : ImageOptionsBase, IDisposable, IHasXmpData, IHasMetadata, ICloneable
{
[JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAndIgnore)]
public override string Format => "GIF";
}
Inheritance
object ← DisposableObject ← ImageOptionsBase ← GifOptions
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.
string dir = "c:\\temp\\";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.gif"))
{
image.Save(dir + "output.bmp", new Aspose.Imaging.ImageOptions.BmpOptions());
image.Save(dir + "output.jpg", new Aspose.Imaging.ImageOptions.JpegOptions());
image.Save(dir + "output.png", new Aspose.Imaging.ImageOptions.PngOptions());
image.Save(dir + "output.tif", new Aspose.Imaging.FileFormats.Tiff.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.
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;
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.
using System.IO;
using Aspose.Imaging;
MemoryStream stream = new MemoryStream();
ImageOptions.GifOptions gifOptions = new ImageOptions.GifOptions { Source = new Sources.StreamSource(stream) };
using (RasterImage image = (RasterImage)Image.Create(gifOptions, 500, 500))
{
Aspose.Imaging.Color[] pixels = image.LoadPixels(image.Bounds);
for (int index = 0; index < pixels.Length; index++)
{
if (index % 2 == 0)
{
pixels[index] = Color.Yellow;
}
else
{
pixels[index] = Color.Blue;
}
}
image.SavePixels(image.Bounds, pixels);
image.Save();
using (FileStream fileStream = new FileStream(@"C:\temp\output.gif", FileMode.Create))
{
stream.WriteTo(fileStream);
}
}
Constructors
GifOptions()
Initializes a new instance of the Aspose.Imaging.ImageOptions.GifOptions class.
public class GifOptions
{
[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.
public class MyClass
{
[JsonProperty]
public Color BackgroundColor { get; set; }
}
Property Value
BackgroundColorIndex
Gets or sets the GIF background color index.
public byte BackgroundColorIndex
{
get;
set;
}
Property Value
ColorResolution
Gets or sets the GIF color resolution.
public byte ColorResolution
{
get;
set;
}
Property Value
Examples
This example shows how to save a BMP image to GIF format using various options.
string dir = "c:\\temp\\";
using (Aspose.Imaging.Image bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(1000, 1000))
{
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();
saveOptions.ColorResolution = 7;
saveOptions.DoPaletteCorrection = true;
saveOptions.Interlaced = true;
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);
}
saveOptions.MaxDiff = 80;
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);
}
}
Remarks
Color Resolution - Number of bits per primary color availableto the original image, minus 1. This value represents the size ofthe entire palette from which the colors in the graphic wereselected, not the number of colors actually used in the graphic.For example, if the value in this field is 3, then the palette ofthe original image had 4 bits per primary color available to createthe image. This value should be set to indicate the richness ofthe original palette, even if not every color from the wholepalette is available on the source machine.
DoPaletteCorrection
Gets or sets a value indicating whether palette correction is applied.
public bool DoPaletteCorrection
{
get;
set;
}
Property Value
Examples
This example shows how to save a BMP image to GIF format using various options.
string dir = "c:\\temp\\";
using (Aspose.Imaging.Image bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(1000, 1000))
{
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();
saveOptions.ColorResolution = 7;
saveOptions.DoPaletteCorrection = true;
saveOptions.Interlaced = true;
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);
}
saveOptions.MaxDiff = 80;
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);
}
}
Remarks
Palette correction means that whenever image is exported to GIF the source image colors will be analyzedin 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.
HasTrailer
Gets or sets a value indicating whether GIF has trailer.
public bool HasTrailer
{
get;
set;
}
Property Value
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
Examples
This example shows how to save a BMP image to GIF format using various options.
string dir = "c:\\temp\\";
using (Aspose.Imaging.Image bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(1000, 1000))
{
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();
saveOptions.ColorResolution = 7;
saveOptions.DoPaletteCorrection = true;
saveOptions.Interlaced = true;
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);
}
saveOptions.MaxDiff = 80;
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);
}
}
IsPaletteSorted
Gets or sets a value indicating whether palette entries are sorted.
public bool IsPaletteSorted
{
get;
set;
}
Property Value
LoopsCount
Gets or sets the loops count (Default 1 loop)
public int LoopsCount
{
get;
set;
}
Property Value
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
Examples
This example shows how to save a BMP image to GIF format using various options.
string dir = "c:\\temp\\";
using (Aspose.Imaging.Image bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(1000, 1000))
{
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();
saveOptions.ColorResolution = 7;
saveOptions.DoPaletteCorrection = true;
saveOptions.Interlaced = true;
using (System.IO.Stream stream = System.IO.File.OpenWrite(dir + "output.gif"))
{
bmpImage.Save(stream, saveOptions);
Console.WriteLine("The size of the lossless GIF: {0} bytes.", stream.Length);
}
saveOptions.MaxDiff = 80;
using (System.IO.Stream stream = System.IO.File.OpenWrite(dir + "output.lossy.gif"))
{
bmpImage.Save(stream, saveOptions);
Console.WriteLine("The size of the lossy GIF: {0} bytes.", stream.Length);
}
}
PixelAspectRatio
Gets or sets the GIF pixel aspect ratio.
public byte PixelAspectRatio
{
get;
set;
}
Property Value
Remarks
Pixel Aspect Ratio - Factor used to compute an approximationof the aspect ratio of the pixel in the original image. If thevalue of the field is not 0, this approximation of the aspect ratiois computed based on the formula:Aspect Ratio = (Pixel Aspect Ratio + 15) / 64The Pixel Aspect Ratio is defined to be the quotient of the pixel’swidth over its height. The value range in this field allowsspecification of the widest pixel of 4:1 to the tallest pixel of1:4 in increments of 1/64th.Values :0 - No aspect ratio information is given.1..255 - Value used in the computation.