Click here to Skip to main content
Licence CPOL
First Posted 1 Sep 2005
Views 577,782
Bookmarked 84 times

NGif, Animated GIF Encoder for .NET

By | 1 Sep 2005 | Article
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)

About the Author

gOODiDEA.NET



China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey19:47 4 Mar '12  
Bug[Bug] Last two pixels of every frame is wrong. Pinmembercodipro2:14 7 Jan '12  
GeneralRe: [Bug] Last two pixels of every frame is wrong. Pinmembermorhost4:51 28 Feb '12  
GeneralTransperency PinmemberJeremiasK7:45 24 Feb '11  
GeneralRe: Transperency Pinmemberv2leon22:49 28 Aug '11  
QuestionSecurity Warning [modified] Pinmemberjedi-samurai21:53 18 Dec '10  
GeneralLZWEncoder Pinmembertom roarty4:59 12 Dec '10  
QuestionGifDecoder.cs exceptions PinmemberDivya Muppa10:16 29 Sep '10  
GeneralTransparency Pinmembergameguy276:39 16 Aug '10  
GeneralBug involving the first frame PinmemberDataflashsabot2:56 23 Jan '10  
GeneralI've released a derived work Pinmembersbridewell10:44 24 Oct '09  
GeneralRe: I've released a derived work Pinmemberjambonbill15:39 3 Nov '09  
General"The process cannot access the file xpto because it is being used by another process". Pinmemberchico@dsi23:52 29 Sep '09  
QuestionCan I release a derived work please? Pinmembersbridewell9:12 4 Sep '09  
AnswerRe: Can I release a derived work please? Pinmemberjambonbill3:04 6 Oct '09  
GeneralRe: Can I release a derived work please? Pinmembersbridewell10:54 6 Oct '09  
GeneralRe: Can I release a derived work please? Pinmemberjambonbill14:50 6 Oct '09  
GeneralRe: Can I release a derived work please? [modified] Pinmembersbridewell9:10 7 Oct '09  
GeneralResize an animated gif background transparency Pinmembervuivuivui21:23 26 Aug '09  
Questionhow to force no transparency PinmemberAreJay7:55 20 Aug '09  
Generalit can't store more than 19 frames. Pinmemberinamgul1:00 26 Feb '09  
GeneralAnimated gif Timeline Properties PinmemberDaffy10122:01 2 Nov '08  
Generalan better implement PinmembergOODiDEA.NET14:54 13 Oct '08  
GeneralRe: an better implement Pinmemberecjob4:24 21 Oct '08  
GeneralRe: an better implement PinmemberSpirch14:13 2 Jan '09  

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.

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