Click here to Skip to main content
15,890,282 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

 
GeneralLZW Patent No. 4,558,302 Pin
Eric P Schneider9-Mar-07 17:53
Eric P Schneider9-Mar-07 17:53 
GeneralRe: LZW Patent No. 4,558,302 Pin
Vlasta_20-Mar-07 1:59
Vlasta_20-Mar-07 1:59 
GeneralRe: LZW Patent No. 4,558,302 Pin
Mons00n22-Mar-07 10:14
Mons00n22-Mar-07 10:14 
GeneralBtw, this is awesome Pin
dB.19-Feb-07 12:12
dB.19-Feb-07 12:12 
GeneralBug: loopcount should be initialized at -1 Pin
dB.19-Feb-07 11:36
dB.19-Feb-07 11:36 
GeneralResizing an animated GIF Pin
dB.19-Feb-07 11:32
dB.19-Feb-07 11:32 
GeneralAccess to the path "c:\test.gif" is denied. Pin
saikatchoudhury20-Dec-06 20:43
saikatchoudhury20-Dec-06 20:43 
QuestionRelease as open source project? PinPopular
jongalloway16-Aug-06 11:33
jongalloway16-Aug-06 11:33 
Would you be interested in releasing this as an open source project? I'd be happy to set it up on SourceForge, CodePlex, or Google Code. The code you've posted works well, but it would be nice to extend it:
- allow alternate quantization methode (Octree, etc.)
- include optimizations (unsafe code, image checksums, etc.)
- leverage System.Drawing
- convert to allow use of individual functions by shifting some of the class variables to method parameters

I've already made some of these changes. It would be great to get your go-ahead to release a next version. I saw that the original Java source allowed reuse for any reason, so if I don't hear back I may just start over from the original Java source.

Jon Galloway
http://weblogs.asp.net/jgalloway
General[Message Deleted] Pin
Damian Wood2-Jul-06 3:35
Damian Wood2-Jul-06 3:35 
GeneralRe: Solution for last 2 pixels of a frame being white Pin
BenBearChen24-Aug-06 18:10
BenBearChen24-Aug-06 18:10 
GeneralAnother Solution ! be tested! Pin
tianlupan224-Aug-06 21:31
tianlupan224-Aug-06 21:31 
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 
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.