views:

178

answers:

2

How do you split an animated gif into its component parts in .net?

Specifically i want to load them into Image(s) (System.Drawing.Image) in memory.

======================

Based on SLaks' answer i now have this

public static IEnumerable<Bitmap> GetImages(Stream stream)
{
 using (var gifImage = Image.FromStream(stream))
 {
  var dimension = new FrameDimension(gifImage.FrameDimensionsList[0]); //gets the GUID
  var frameCount = gifImage.GetFrameCount(dimension); //total frames in the animation
  for (var index = 0; index < frameCount; index++)
  {
   gifImage.SelectActiveFrame(dimension, index); //find the frame
   yield return (Bitmap) gifImage.Clone(); //return a copy of it
  }
 }
}
+1  A: 

Use the SetActiveFrame method to select the active frame of an Image instance holding an animated GIF. For example:

image.SelectActiveFrame(FrameDimension.Time, frameIndex);

To get the number of frames, call GetFrameCount(FrameDimension.Time)

If you just want to play the animation, you can put it into a PictureBox or use the ImageAnimator class.

SLaks
A: 

Semi-related, in WPF you have the BitmapDecoders which will provide you with all the frames of an image.

See BitmapDecoder.Create and BitmapDecoder.Frames.

Frank Krueger