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

 
NewsBugfixed version (updated link) Pin
avianbc6-Jan-22 10:18
avianbc6-Jan-22 10:18 
QuestionMessage Closed Pin
11-Mar-19 20:14
SanjoyNath@GeometrifyingTrigonometry11-Mar-19 20:14 
Questionconvert video to gif like does whatsapp ? Pin
kiquenet.com4-Mar-17 1:50
professionalkiquenet.com4-Mar-17 1:50 
QuestionA major BUG in this project Pin
Member 421131021-Apr-16 21:02
Member 421131021-Apr-16 21:02 
BugTranspraency Support Pin
Nofuzy10-Sep-15 8:37
Nofuzy10-Sep-15 8:37 
QuestionSet Different Delay times Pin
Nofuzy21-Aug-15 15:04
Nofuzy21-Aug-15 15:04 
AnswerRe: Set Different Delay times Pin
John Schroedl10-Sep-15 8:16
professionalJohn Schroedl10-Sep-15 8:16 
QuestionNice but slow Pin
Andy Cartwright20-Aug-15 19:43
Andy Cartwright20-Aug-15 19:43 
SuggestionThis works, but there's a better and much faster library (Bumpkit) Pin
Member 112479846-Mar-15 0:59
professionalMember 112479846-Mar-15 0:59 
GeneralRe: This works, but there's a better and much faster library (Bumpkit) Pin
kiquenet.com4-Mar-17 1:53
professionalkiquenet.com4-Mar-17 1:53 
GeneralMy vote of 1 Pin
Member 1007116316-Aug-14 2:37
Member 1007116316-Aug-14 2:37 
QuestionArithmetic operation resulted in an overflow. Pin
Joshua Galloway8-Aug-14 4:53
Joshua Galloway8-Aug-14 4:53 
QuestionGreat tool, saved me a lot of work! Pin
mingonn29-Jan-14 13:58
mingonn29-Jan-14 13:58 
GeneralGreate Job :) Pin
VinaymSharma12-Dec-13 0:51
VinaymSharma12-Dec-13 0:51 
Questionsize limit? Pin
Member 39966278-Nov-13 9:47
Member 39966278-Nov-13 9:47 
AnswerRe: size limit? Pin
Nicke Manarin21-Feb-14 11:43
Nicke Manarin21-Feb-14 11:43 
AnswerRe: size limit? Pin
Member 106623506-Jun-14 9:42
Member 106623506-Jun-14 9:42 
QuestionFileStream vs Stream Pin
Member 1033860916-Oct-13 6:32
Member 1033860916-Oct-13 6:32 
AnswerRe: FileStream vs Stream Pin
Nicke Manarin22-Oct-13 8:01
Nicke Manarin22-Oct-13 8:01 
QuestionI get black backgrounds. Has anyone ever come up with a solution for this? Pin
Member 946120617-Sep-13 20:39
Member 946120617-Sep-13 20:39 
SuggestionDelay method Pin
Sylvainguer17-Jul-13 21:58
Sylvainguer17-Jul-13 21:58 
QuestionHow do i add this to my project? Pin
droidap24-May-13 8:16
droidap24-May-13 8:16 
AnswerRe: How do i add this to my project? Pin
Member 1029123023-Sep-13 0:33
Member 1029123023-Sep-13 0:33 
QuestionI created a new dll file which is a wrapper from c++ of a gif animation if you want Pin
chocolade18-May-13 4:32
chocolade18-May-13 4:32 
QuestionSystem.Windows.Media.Imaging.GifBitmapEncoder Pin
Brian Herbert16-May-13 12:14
Brian Herbert16-May-13 12:14 
This is nice to know about but what is wrong with the built in .NET class?

I am using it from a WPF app but have not worked out how to make the .gif repeat over and over. It just goes through the frames once and then stops.
Pie Man

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.