18

I'm a beginner in C#. I would like to know if there's a way to access different frames inside a GIF animation with C#. I'm using Visual Studio 2008.

Valeria
  • 183
  • 1
  • 2
  • 6

2 Answers2

28

Try this:

using System.Drawing;    
using System.Drawing.Imaging;

Image gifImg = Image.FromFile(pathToGifFile);
FrameDimension dimension = new FrameDimension(gifImg.FrameDimensionsList[0]);
// Number of frames
int frameCount = gifImg.GetFrameCount(dimension);
// Return an Image at a certain index
gifImg.SelectActiveFrame(dimension, index);
Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
Renaud Bompuis
  • 16,596
  • 4
  • 56
  • 86
  • 1
    What part of .net are Image and FrameDimension found in? – aj.toulan Oct 05 '15 at 20:30
  • 4
    @aj.toulan For Image, System.Drawing; for FrameDimension, System.Drawing.Imaging. – soulblazer Oct 11 '15 at 05:32
  • Just to say if you are using Visual Studio place the cursor on the type that doesn't have the namespace included and press "Ctrl+." , if you have the right namespace added already as a reference in your project it will give you an option in the drop-down to auto add the namespace decoration. – Sam Apr 25 '16 at 10:22
  • isn't SelectActiveFrame return an integer? – AaA Aug 03 '18 at 07:42
  • @AaA after calling SelectActiveFrame you simply draw or use the Image normally. The object's internals will ensure the specified frame's bitmap is used. – DAG Jan 17 '20 at 19:31
9

a bit of googling: editing animated gif's in c#

You can read the animated Gif with Image.GetFrameCount() and SelectActiveFrame().

JoanComasFdz
  • 2,911
  • 5
  • 34
  • 50
RvdK
  • 19,580
  • 4
  • 64
  • 107
  • This answer also includes information about getting the timing information out in additional to the frames – John Aug 04 '09 at 11:59
  • I would not recommend the answer as a good one, generally speaking. You do not need to make a bitmap every time you draw or use a frame. The SelectActiveFrame ensures that when the image is drawn, the specified frame will be used. – DAG Jan 17 '20 at 19:17