Click here to Skip to main content
15,891,607 members
Articles / Mobile Apps
Article

NGif, Animated GIF Encoder for .NET

Rate me:
Please Sign up or sign in to vote.
4.02/5 (58 votes)
1 Sep 2005CPOL 1.5M   18K   117   123
Create animated GIF images using C#.

Sample Image - NGif.gif

Introduction

Because .NET Framework can't create animated GIF images, NGif provides a way to create GIF animations in the .NET framework. It can create an animated GIF from several images and extract images from an animated GIF.

Using the code

C#
/* create Gif */
//you should replace filepath
String [] imageFilePaths = new String[]{"c:\\01.png","c:\\02.png","c:\\03.png"}; 
String outputFilePath = "c:\\test.gif";
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.Start( outputFilePath );
e.SetDelay(500);
//-1:no repeat,0:always repeat
e.SetRepeat(0);
for (int i = 0, count = imageFilePaths.Length; i < count; i++ ) 
{
 e.AddFrame( Image.FromFile( imageFilePaths[i] ) );
}
e.Finish();
/* extract Gif */
string outputPath = "c:\\";
GifDecoder gifDecoder = new GifDecoder();
gifDecoder.Read( "c:\\test.gif" );
for ( int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++ ) 
{
 Image frame = gifDecoder.GetFrame( i ); // frame i
 frame.Save( outputPath + Guid.NewGuid().ToString() 
                       + ".png", ImageFormat.Png );
}

Points of Interest

Use Stream to replace BinaryWriter when you write a fixed-byte structured binary file.

History

  • 31 Aug 2005: Draft.

License

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


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralBUG! Last pixel (bottom-right) always white! (+) Pin
dtrofimov25-Feb-06 9:57
dtrofimov25-Feb-06 9:57 
GeneralRe: BUG! Last pixel (bottom-right) always white! (+) Pin
morhost28-Feb-12 4:45
morhost28-Feb-12 4:45 
QuestionBottom Right Corner Problem? Pin
RipplingCreek4-Feb-06 8:07
RipplingCreek4-Feb-06 8:07 
GeneralAdded output to a MemoryStream Pin
tgiphil1-Sep-05 22:59
tgiphil1-Sep-05 22:59 
GeneralRe: Added output to a MemoryStream Pin
merto22-Jan-06 8:47
merto22-Jan-06 8:47 
GeneralRe: Added output to a MemoryStream Pin
ssh59110-Dec-06 20:21
ssh59110-Dec-06 20:21 
GeneralRe: Added output to a MemoryStream Pin
dB.19-Feb-07 11:33
dB.19-Feb-07 11:33 
GeneralSuggestions Pin
The_Mega_ZZTer1-Sep-05 17:46
The_Mega_ZZTer1-Sep-05 17:46 
Does this class do any sort of opitmization? EX:
- Palette optimization, where if each frame has the same palette it doesn't have to be stored for each frame, but for the global GIF instead (GIF format allows for this right?)
- Bounding boxes that define what part of the image changes from the previous frame, so a whole frame does not need to be stored if only a little piece changes (speeds up rendering, too!)

Also is it possible to set the frame delay PER FRAME using your class? The GIF format allows this, and it is something that freeware GIF animators like UnFreez lack.

For that last piece, if not I'll probably look at the code and mod it myself to do that. Probably also code the ability to extract frame delay times if it's not already in there.

The other two would be out of my league, as I don't know that much about the GIF format to code those, unfortunately.

Looking at your example a bit closer... it might take a bit of tweaking to get it to work like other .NET classes, but I think it would be well worth the effort.
QuestionRe: Suggestions Pin
ammarmujeeb30-Jan-06 2:10
ammarmujeeb30-Jan-06 2:10 
AnswerRe: Suggestions Pin
tianlupan214-Aug-06 5:45
tianlupan214-Aug-06 5:45 
AnswerRe: Suggestions Pin
pedro.pandre17-Aug-06 1:56
pedro.pandre17-Aug-06 1:56 
GeneralRe: Suggestions Pin
pedro.pandre17-Aug-06 2:03
pedro.pandre17-Aug-06 2:03 
AnswerRe: Suggestions [modified] Pin
Phoenixillusion2-Mar-07 23:08
Phoenixillusion2-Mar-07 23:08 
GeneralRe: Suggestions Pin
Jim Hunt15-Jun-07 8:22
Jim Hunt15-Jun-07 8:22 

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.