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

 
GeneralBugfix NextPixel() Pin
Member 21880516-Aug-08 23:49
Member 21880516-Aug-08 23:49 
GeneralRe: Bugfix NextPixel() Pin
PaulNeyman11-Jan-10 1:08
PaulNeyman11-Jan-10 1:08 
GeneralExcellent work! Pin
DigiOz Multimedia18-Jun-08 18:47
DigiOz Multimedia18-Jun-08 18:47 
QuestionThe gif be changed bigger? Pin
tintown_liu25-Dec-07 14:35
tintown_liu25-Dec-07 14:35 
AnswerRe: The gif be changed bigger? Pin
Shobin Mathew9-Jul-08 18:45
Shobin Mathew9-Jul-08 18:45 
Generalits important [modified] Pin
ganesh154-Nov-07 20:18
ganesh154-Nov-07 20:18 
GeneralRe: its important Pin
Shobin Mathew9-Jul-08 18:41
Shobin Mathew9-Jul-08 18:41 
GeneralBug fixed Pin
chuanchu13-Aug-07 15:23
chuanchu13-Aug-07 15:23 
Need to modify two methods in GifDecoder.cs:

int [] GetPixels( Bitmap bitmap )
{
int [] pixels = new int [image.Width * image.Height];
int count = 0;
for (int th = 0; th < image.Height; th++)
{
for (int tw = 0; tw < image.Width; tw++)
{
Color color = bitmap.GetPixel(tw, th);
pixels[count++] = color.ToArgb();
}
}
return pixels;
}

protected void SetPixels()
{
// expose destination image's pixels as int array
// int[] dest =
// (( int ) image.getRaster().getDataBuffer()).getData();
//int[] dest = GetPixels( bitmap );

int[] dest = null;

// fill in starting image contents based on last image's dispose code
if (lastDispose > 0)
{
if (lastDispose == 3)
{
// use image before last
int n = frameCount - 2;
if (n > 0)
{
lastImage = GetFrame(n - 1);
}
else
{
lastImage = null;
}
}
if (lastImage != null)
{
if (lastDispose == 2)
{
// fill last image rect area with background color
lastImage = new Bitmap(lastImage);
Graphics g = Graphics.FromImage(lastImage);
Color c = Color.Empty;
if (transparency)
{
c = Color.FromArgb(255, 0, 0, 0); // assume background is transparent
}
else
{
c = Color.FromArgb( lastBgColor ) ;
// c = new Color(lastBgColor); // use given background color
}
Brush brush = new SolidBrush( c );
g.FillRectangle( brush, lastRect );
brush.Dispose();
g.Dispose();
}
dest = GetPixels((Bitmap)lastImage);
}
}

if (dest == null) dest = new int[bitmap.Width * bitmap.Height];

// copy each source line to the appropriate place in the destination
int pass = 1;
int inc = 8;
int iline = 0;
for (int i = 0; i < ih; i++)
{
int line = i;
if (interlace)
{
if (iline >= ih)
{
pass++;
switch (pass)
{
case 2 :
iline = 4;
break;
case 3 :
iline = 2;
inc = 4;
break;
case 4 :
iline = 1;
inc = 2;
break;
}
}
line = iline;
iline += inc;
}
line += iy;
if (line < height)
{
int k = line * width;
int dx = k + ix; // start of line in dest
int dlim = dx + iw; // end of dest line
if ((k + width) < dlim)
{
dlim = k + width; // past dest edge
}
int sx = i * iw; // start of line in source
while (dx < dlim)
{
// map color and insert in destination
int index = ((int) pixels[sx++]) & 0xff;
int c = act[index];
if (c != 0)
{
dest[dx] = c;
}
dx++;
}
}
}
SetPixels( dest );
}

I am also try to use LockBits in GetPixels and SetPixels, without using unsafe code. I have found a way, but need some time to finish it.
GeneralRe: Bug fixed Pin
Adnan8365-Oct-07 0:34
Adnan8365-Oct-07 0:34 
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 
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 

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.