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

 
Bug[Bug] Last two pixels of every frame is wrong. Pin
codipro7-Jan-12 2:14
codipro7-Jan-12 2:14 
GeneralRe: [Bug] Last two pixels of every frame is wrong. Pin
morhost28-Feb-12 4:51
morhost28-Feb-12 4:51 
GeneralRe: [Bug] Last two pixels of every frame is wrong. Pin
Member 1034753121-Oct-13 8:23
Member 1034753121-Oct-13 8:23 
GeneralTransperency Pin
JeremiasK24-Feb-11 7:45
JeremiasK24-Feb-11 7:45 
GeneralRe: Transperency Pin
v2leon28-Aug-11 22:49
v2leon28-Aug-11 22:49 
GeneralRe: Transperency Pin
maoshu22-Dec-12 0:14
maoshu22-Dec-12 0:14 
GeneralRe: Transperency Pin
Chase Viking4-Jun-13 4:48
Chase Viking4-Jun-13 4:48 
QuestionSecurity Warning [modified] Pin
jedi-samurai18-Dec-10 21:53
jedi-samurai18-Dec-10 21:53 
Software i developed with this Gif.Components.dll is deleted by security soft (Norton 2010) warning with high level risk. There was no warning with other security soft (Mcafee).

Is anyone encountered with this kind of problem with this dll?

-----------------

There is no warning with Debug file, only Release file.

Visual Studio 2008
Windows7
modified on Monday, December 20, 2010 8:45 AM

AnswerRe: Security Warning [modified] Pin
Jajnick1-Sep-12 5:14
Jajnick1-Sep-12 5:14 
GeneralLZWEncoder Pin
tom roarty12-Dec-10 4:59
tom roarty12-Dec-10 4:59 
QuestionGifDecoder.cs exceptions Pin
Divya Muppa29-Sep-10 10:16
Divya Muppa29-Sep-10 10:16 
GeneralTransparency Pin
gameguy2716-Aug-10 6:39
gameguy2716-Aug-10 6:39 
GeneralBug involving the first frame Pin
Dataflashsabot23-Jan-10 2:56
Dataflashsabot23-Jan-10 2:56 
GeneralI've released a derived work Pin
sbridewell24-Oct-09 10:44
sbridewell24-Oct-09 10:44 
GeneralRe: I've released a derived work Pin
jambonbill3-Nov-09 15:39
jambonbill3-Nov-09 15:39 
General"The process cannot access the file xpto because it is being used by another process". Pin
frantic029-Sep-09 23:52
professionalfrantic029-Sep-09 23:52 
GeneralRe: "The process cannot access the file xpto because it is being used by another process". Pin
Rassler481-Mar-13 1:16
Rassler481-Mar-13 1:16 
QuestionCan I release a derived work please? Pin
sbridewell4-Sep-09 9:12
sbridewell4-Sep-09 9:12 
AnswerRe: Can I release a derived work please? Pin
jambonbill6-Oct-09 3:04
jambonbill6-Oct-09 3:04 
GeneralRe: Can I release a derived work please? Pin
sbridewell6-Oct-09 10:54
sbridewell6-Oct-09 10:54 
GeneralRe: Can I release a derived work please? Pin
jambonbill6-Oct-09 14:50
jambonbill6-Oct-09 14:50 
GeneralRe: Can I release a derived work please? [modified] Pin
sbridewell7-Oct-09 9:10
sbridewell7-Oct-09 9:10 
GeneralResize an animated gif background transparency Pin
vuivuivui26-Aug-09 21:23
vuivuivui26-Aug-09 21:23 
Questionhow to force no transparency Pin
Are Jay20-Aug-09 7:55
Are Jay20-Aug-09 7:55 
Generalit can't store more than 19 frames. Pin
inamgul26-Feb-09 1:00
inamgul26-Feb-09 1:00 

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.