Reputation: 659
I have used the sample code as found in the answer at how to create an animated gif in .net
ie
System.Windows.Media.Imaging.GifBitmapEncoder gEnc = new GifBitmapEncoder();
foreach (System.Drawing.Bitmap bmpImage in images)
{
var src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmpImage.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
gEnc.Frames.Add(BitmapFrame.Create(src));
}
using(FileStream fs = new FileStream(path, FileMode.Create))
{
gEnc.Save(fs);
}
I would now like to set the speed of the animate gif. The help in this area is terrible. Anyone got any idea of how to do it? This must surely be possible. It seems to be such a basic function of an animated gif but they seem to have gone out of their way to make it difficult.
Thanks in advance.
Upvotes: 2
Views: 3253
Reputation: 292415
I don't think you can do it with GifBitmapEncoder
. Theoretically, you could use BitmapMetadata.SetQuery
to set the value of /grctlext/Delay
on each frame (the delay is specified in the Graphic Control Extension of each frame, as explained in the Wikipedia article). But the doc says:
Graphics Interchange Format (GIF) images do not support global preview, global thumbnails, global metadata, frame level thumbnails, or frame level metadata.
Which is technically incorrect, because the GIF format itself does support global and frame-level metadata; it's just the GifBitmapEncoder
class that doesn't support it.
So I think your only options are to use another, more complete image manipulation library, or to do it manually. The GIF format is rather simple; it's described in details on this site, including the parts about animation and LZW compression.
Alternatively, instead of doing the whole encoding yourself, you could use GifBitmapEncoder
to do the heavy lifting, and just patch the metadata in the resuting stream afterwards. The code in my XamlAnimatedGif library might help; you won't be able to use it directly, but you could reuse some parts of it, as it implements a full GIF decoder.
Upvotes: 3