Click here to Skip to main content
15,881,248 members
Articles / All Topics
Technical Blog

C# Get Frames from a GIF

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
2 Aug 2014CPOL1 min read 18.4K   6   2
This is a simple method to extract a certain frame (or all of them) from a GIF, using C#.
This is a simple method to extract a certain frame (or all of them) from a GIF, using C#. As always, .NET provides all the functions we need, so it shouldn't take more than 12 lines of code.


Basic Information



As you know, GIFs contain various images (frames) that are displayed one by one after a certain time interval (unlike TIFFs, that display all the frames simultaneously in one picture). However we'll be working with the GIF format (whose frames are entirely based on the time dimension).


In order to get the number of frames we'll use GetFrameCount(FrameDimension.Time), which returns an int. Note that it requires an argument that specifies the dimension.

Next, we have to iterate through each frame and then select it using the same dimension and an index (SelectActiveFrame(FrameDimension.Time, indexOfCurrentFrame)).

Important: This method modifies the original image, so we'll need to call Clone() on this object and cast it as an Image before saving it (otherwise we'd just save the GIF - not the current frame).


Example



This small function extracts & returns an array of frames (Image), from a given picture.

* Recommend executing this in a worker thread, especially when GIFs have many frames.

Image[] getFrames(Image originalImg)<br />
{<br />
    int numberOfFrames = originalImg.GetFrameCount(FrameDimension.Time);<br />
    Image[] frames = new Image[numberOfFrames];<br />
<br />
    for (int i = 0; i < numberOfFrames; i++)<br />
    {<br />
        originalImg.SelectActiveFrame(FrameDimension.Time, i);<br />
        frames[i] = ((Image)originalImg.Clone());<br />
    }<br />
<br />
    return frames;<br />
}



It can be called like this:
Image[] frames = getFrames(Image.FromFile("random.gif"));



This article was originally posted at http://www.codingvision.net/files/c-get-frames-from-a-gif

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Romania Romania
Master's student @ ACS / UPB (Advanced Cybersecurity), Graduate Teaching Assistant, Junior Security Researcher. Also, webmaster of coding.vision

Comments and Discussions

 
QuestionUnfortunately... Pin
Nyerguds2-May-17 10:43
Nyerguds2-May-17 10:43 
GeneralMy vote of 5 Pin
Carsten V2.03-Aug-14 7:20
Carsten V2.03-Aug-14 7:20 
Nice solution!

* Recommend executing this in a worker thread, especially when GIFs have many frames.
Good advice! Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.