ImageExtensionsSaveAsAnimatedGif(IEnumerableImage, Stream, IEnumerableTimeSpan, IQuantizer, IDitherer) Method

Saves the provided frames as a looping GIF animation into the specified Stream. When Image instances in frames already contain multiple frames, only the current frame is taken.

Definition

Namespace: KGySoft.Drawing
Assembly: KGySoft.Drawing (in KGySoft.Drawing.dll) Version: 8.1.0
C#
public static void SaveAsAnimatedGif(
	this IEnumerable<Image> frames,
	Stream stream,
	IEnumerable<TimeSpan>? delays,
	IQuantizer? quantizer = null,
	IDitherer? ditherer = null
)

Parameters

frames  IEnumerableImage
The frames to save into the GIF data stream.
stream  Stream
The stream into the GIF data is to be saved.
delays  IEnumerableTimeSpan
The collection of the delays to be used for the animation. If or empty, then a default 100 ms delay will be used for all frames. If contains less elements than frames, then the last value will be re-used for the remaining frames.
quantizer  IQuantizer  (Optional)
An optional quantizer to be used for the frames. If , then for frames with a non-indexed pixel format a quantizer returned by the OptimizedPaletteQuantizer.Wu method will be used. This parameter is optional.
Default value: .
ditherer  IDitherer  (Optional)
An optional ditherer to be used when quantizing the frames. This parameter is optional.
Default value: .

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableImage. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

  Note

This method adjusts the degree of parallelization automatically, blocks the caller, and does not support cancellation or reporting progress. Use the GifEncoder.BeginEncodeAnimation or GifEncoder.EncodeAnimationAsync (in .NET Framework 4.0 and above) methods for asynchronous call and to adjust parallelization, set up cancellation and for reporting progress.

When frames contain multi-frame instances, this method takes only the current frame. You can extract images by the ExtractBitmaps extension method.

The enumerator of frames is evaluated in a lazy manner. You can even dispose the previous image once the next one is queried.

Though this method does not support reporting progress directly, you can pass an iterator to the frames parameter that can track how many images have already been processed.

The resolution of the animation is determined by the first frame. If subsequent frames have different sizes, then they will be centered.

If quantizer supports an optimized palette for each frames (like OptimizedPaletteQuantizer), then some frames of the animation might have even more than 256 colors (depending on the differences between frames).

  Tip

To customize looping mode, frame size handling, delta frames strategy, etc., then use directly the GifEncoder class and its EncodeAnimation method. And for low level encoding you can instantiate the GifEncoder class and add the frames manually.

See Also