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

 
GeneralRe: Bug fixed Pin
cmhienng27-Nov-07 21:33
cmhienng27-Nov-07 21:33 
GeneralRe: Bug fixed Pin
xzhang19-Mar-09 8:11
xzhang19-Mar-09 8:11 
GeneralTransparency Not Working Pin
Jim Hunt15-Jun-07 8:21
Jim Hunt15-Jun-07 8:21 
GeneralRe: Transparency Not Working Pin
chuanchu13-Aug-07 15:10
chuanchu13-Aug-07 15:10 
GeneralRe: Transparency Not Working Pin
Adnan8365-Oct-07 1:28
Adnan8365-Oct-07 1:28 
GeneralRe: Transparency Not Working Pin
xftan20-Oct-07 10:38
xftan20-Oct-07 10:38 
GeneralRe: Transparency Not Working Pin
Shobin Mathew10-Jul-08 3:06
Shobin Mathew10-Jul-08 3:06 
GeneralRe: Transparency Not Working Pin
Equinox SE30 Jota26-Oct-11 15:24
Equinox SE30 Jota26-Oct-11 15:24 
Hi,

Your fix so nearly almost worked! The bottom-right pixel of the resulting animation appears to flicker through a selection of colours. Any ideas on how to fix this?

Edit: I've just also noticed parts of the image becoming transparent which weren't originally transparent. I'm guessing this is something to do with the colour of those parts matching the transparency colour. Does this mean having to write an algorithm to find an unused colour in all frames prior to adding them?
GeneralRe: Transparency Not Working Pin
Chase Viking4-Jun-13 5:01
Chase Viking4-Jun-13 5:01 
GeneralRe: Transparency Not Working Pin
Member 946120618-Sep-13 0:25
Member 946120618-Sep-13 0:25 
GeneralBug? Net CF Pin
Hoar Wu31-May-07 15:55
Hoar Wu31-May-07 15:55 
GeneralRe: Bug? Net CF Pin
chuanchu13-Aug-07 15:12
chuanchu13-Aug-07 15:12 
GeneralRe: Bug? Net CF Pin
nixkuroi7-Jun-14 14:44
nixkuroi7-Jun-14 14:44 
QuestionHas anybody been able to optimize this code? Pin
SubodhShakya23-Apr-07 20:50
SubodhShakya23-Apr-07 20:50 
AnswerRe: Has anybody been able to optimize this code? Pin
Ephoy9-May-07 1:29
Ephoy9-May-07 1:29 
GeneralRe: Has anybody been able to optimize this code? Pin
User 4886924-Sep-07 3:32
User 4886924-Sep-07 3:32 
QuestionRe: Has anybody been able to optimize this code? Pin
Ephoy14-Oct-07 7:22
Ephoy14-Oct-07 7:22 
AnswerRe: Has anybody been able to optimize this code? Pin
User 4886914-Oct-07 23:04
User 4886914-Oct-07 23:04 
GeneralRe: Has anybody been able to optimize this code? Pin
TODarkone18-Nov-07 9:15
TODarkone18-Nov-07 9:15 
QuestionPlay a gif in a Form? Pin
anderslundsgard22-Mar-07 21:41
anderslundsgard22-Mar-07 21:41 
AnswerRe: Play a gif in a Form? Pin
yuxuetaoxp8-Jan-08 3:21
yuxuetaoxp8-Jan-08 3:21 
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 

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.