Click here to Skip to main content
11,412,833 members (75,325 online)
Click here to Skip to main content

NGif, Animated GIF Encoder for .NET

, 1 Sep 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
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

/* 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)

Share

About the Author

gOODiDEA.NET

China China
No Biography provided

Comments and Discussions

 
SuggestionThis works, but there's a better and much faster library (Bumpkit) Pin
Member 11247984 at 6-Mar-15 1:59
memberMember 112479846-Mar-15 1:59 
GeneralMy vote of 1 Pin
Member 10071163 at 16-Aug-14 3:37
memberMember 1007116316-Aug-14 3:37 
QuestionArithmetic operation resulted in an overflow. Pin
Joshua Galloway at 8-Aug-14 5:53
memberJoshua Galloway8-Aug-14 5:53 
QuestionGreat tool, saved me a lot of work! Pin
mingonn at 29-Jan-14 14:58
membermingonn29-Jan-14 14:58 
GeneralGreate Job :) Pin
Vinaymm at 12-Dec-13 1:51
memberVinaymm12-Dec-13 1:51 
Questionsize limit? Pin
Member 3996627 at 8-Nov-13 10:47
memberMember 39966278-Nov-13 10:47 
AnswerRe: size limit? Pin
Nicke Da Silva Manarin at 21-Feb-14 12:43
memberNicke Da Silva Manarin21-Feb-14 12:43 
AnswerRe: size limit? Pin
Member 10662350 at 6-Jun-14 10:42
memberMember 106623506-Jun-14 10:42 
QuestionFileStream vs Stream Pin
Member 10338609 at 16-Oct-13 7:32
memberMember 1033860916-Oct-13 7:32 
AnswerRe: FileStream vs Stream Pin
Nicke Da Silva Manarin at 22-Oct-13 9:01
memberNicke Da Silva Manarin22-Oct-13 9:01 
QuestionI get black backgrounds. Has anyone ever come up with a solution for this? Pin
Member 9461206 at 17-Sep-13 21:39
memberMember 946120617-Sep-13 21:39 
SuggestionDelay method Pin
Sylvainguer at 17-Jul-13 22:58
memberSylvainguer17-Jul-13 22:58 
QuestionHow do i add this to my project? Pin
droidap at 24-May-13 9:16
memberdroidap24-May-13 9:16 
AnswerRe: How do i add this to my project? Pin
Member 10291230 at 23-Sep-13 1:33
memberMember 1029123023-Sep-13 1:33 
QuestionI created a new dll file which is a wrapper from c++ of a gif animation if you want Pin
chocolade at 18-May-13 5:32
memberchocolade18-May-13 5:32 
QuestionSystem.Windows.Media.Imaging.GifBitmapEncoder Pin
Pie At The Raven at 16-May-13 13:14
memberPie At The Raven16-May-13 13:14 
AnswerRe: System.Windows.Media.Imaging.GifBitmapEncoder Pin
Member 10291230 at 23-Sep-13 1:31
memberMember 1029123023-Sep-13 1:31 
QuestionNot properly disposing/closing images Pin
rdavidson22 at 8-Jul-12 12:26
memberrdavidson228-Jul-12 12:26 
QuestionThank you Pin
Member 8357513 at 15-Jun-12 17:30
memberMember 835751315-Jun-12 17:30 
QuestionBugfixed version Pin
avianbc at 11-Jun-12 15:22
memberavianbc11-Jun-12 15:22 
AnswerRe: Bugfixed version Pin
class_Volrath at 28-Jun-14 13:19
memberclass_Volrath28-Jun-14 13:19 
AnswerRe: Bugfixed version Pin
horsedrowner at 30-Jun-14 8:13
memberhorsedrowner30-Jun-14 8:13 
GeneralMy vote of 5 Pin
manoj kumar choubey at 4-Mar-12 20:47
membermanoj kumar choubey4-Mar-12 20:47 
Bug[Bug] Last two pixels of every frame is wrong. Pin
codipro at 7-Jan-12 3:14
membercodipro7-Jan-12 3:14 
GeneralRe: [Bug] Last two pixels of every frame is wrong. Pin
morhost at 28-Feb-12 5:51
membermorhost28-Feb-12 5:51 
The bug is in class LZWEncoder, method NextPixel()...

Here is the fix:

private int NextPixel()
{
  if (curPixel <= pixAry.GetUpperBound(0))
  {
    byte pix = pixAry[curPixel++];
    return pix && 0xff;
  }
  else
    return (EOF);
}

Variable remaining in the original code is unuseful and can be removed at all.
GeneralRe: [Bug] Last two pixels of every frame is wrong. Pin
Member 10347531 at 21-Oct-13 9:23
memberMember 1034753121-Oct-13 9:23 
GeneralTransperency Pin
JeremiasK at 24-Feb-11 8:45
memberJeremiasK24-Feb-11 8:45 
GeneralRe: Transperency Pin
v2leon at 28-Aug-11 23:49
memberv2leon28-Aug-11 23:49 
GeneralRe: Transperency Pin
maoshu at 22-Dec-12 1:14
membermaoshu22-Dec-12 1:14 
GeneralRe: Transperency Pin
Froogabadoodle at 4-Jun-13 5:48
memberFroogabadoodle4-Jun-13 5:48 
QuestionSecurity Warning [modified] Pin
jedi-samurai at 18-Dec-10 22:53
memberjedi-samurai18-Dec-10 22:53 
AnswerRe: Security Warning [modified] Pin
Jajnick at 1-Sep-12 6:14
memberJajnick1-Sep-12 6:14 
GeneralLZWEncoder Pin
tom roarty at 12-Dec-10 5:59
membertom roarty12-Dec-10 5:59 
QuestionGifDecoder.cs exceptions Pin
Divya Muppa at 29-Sep-10 11:16
memberDivya Muppa29-Sep-10 11:16 
GeneralTransparency Pin
gameguy27 at 16-Aug-10 7:39
membergameguy2716-Aug-10 7:39 
GeneralBug involving the first frame Pin
Dataflashsabot at 23-Jan-10 3:56
memberDataflashsabot23-Jan-10 3:56 
GeneralI've released a derived work Pin
sbridewell at 24-Oct-09 11:44
membersbridewell24-Oct-09 11:44 
GeneralRe: I've released a derived work Pin
jambonbill at 3-Nov-09 16:39
memberjambonbill3-Nov-09 16:39 
General"The process cannot access the file xpto because it is being used by another process". Pin
chico@dsi at 30-Sep-09 0:52
memberchico@dsi30-Sep-09 0:52 
GeneralRe: "The process cannot access the file xpto because it is being used by another process". Pin
Rassler48 at 1-Mar-13 2:16
memberRassler481-Mar-13 2:16 
QuestionCan I release a derived work please? Pin
sbridewell at 4-Sep-09 10:12
membersbridewell4-Sep-09 10:12 
AnswerRe: Can I release a derived work please? Pin
jambonbill at 6-Oct-09 4:04
memberjambonbill6-Oct-09 4:04 
GeneralRe: Can I release a derived work please? Pin
sbridewell at 6-Oct-09 11:54
membersbridewell6-Oct-09 11:54 
GeneralRe: Can I release a derived work please? Pin
jambonbill at 6-Oct-09 15:50
memberjambonbill6-Oct-09 15:50 
GeneralRe: Can I release a derived work please? [modified] Pin
sbridewell at 7-Oct-09 10:10
membersbridewell7-Oct-09 10:10 
GeneralResize an animated gif background transparency Pin
vuivuivui at 26-Aug-09 22:23
membervuivuivui26-Aug-09 22:23 
Questionhow to force no transparency Pin
AreJay at 20-Aug-09 8:55
memberAreJay20-Aug-09 8:55 
Generalit can't store more than 19 frames. Pin
inamgul at 26-Feb-09 2:00
memberinamgul26-Feb-09 2:00 
GeneralAnimated gif Timeline Properties Pin
Daffy101 at 2-Nov-08 23:01
memberDaffy1012-Nov-08 23:01 
Generalan better implement Pin
gOODiDEA.NET at 13-Oct-08 15:54
membergOODiDEA.NET13-Oct-08 15:54 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.1 | Last Updated 1 Sep 2005
Article Copyright 2005 by gOODiDEA.NET
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid