Click here to Skip to main content
14,036,509 members
Click here to Skip to main content
Add your own
alternative version

Stats

1.3M views
16.2K downloads
117 bookmarked
Posted 1 Sep 2005
Licenced CPOL

NGif, Animated GIF Encoder for .NET

, 1 Sep 2005
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

You may also be interested in...

Comments and Discussions

 
GeneralRe: Transparency Not Working Pin
Equinox SE30 Jota26-Oct-11 15:24
memberEquinox SE30 Jota26-Oct-11 15:24 
GeneralRe: Transparency Not Working Pin
Chase Viking4-Jun-13 5:01
memberChase Viking4-Jun-13 5:01 
GeneralRe: Transparency Not Working Pin
Member 946120618-Sep-13 0:25
memberMember 946120618-Sep-13 0:25 
GeneralBug? Net CF Pin
Hoar Wu31-May-07 15:55
memberHoar Wu31-May-07 15:55 
GeneralRe: Bug? Net CF Pin
chuanchu13-Aug-07 15:12
memberchuanchu13-Aug-07 15:12 
GeneralRe: Bug? Net CF Pin
nixkuroi7-Jun-14 14:44
membernixkuroi7-Jun-14 14:44 
QuestionHas anybody been able to optimize this code? Pin
SubodhShakya23-Apr-07 20:50
memberSubodhShakya23-Apr-07 20:50 
AnswerRe: Has anybody been able to optimize this code? Pin
Ephoy9-May-07 1:29
memberEphoy9-May-07 1:29 
I have been able to optimize the GifDecoder quiet a bit, but at the expense of compatability. The realy slow code which runs a lot (realy A LOT) of times is the GetPixel() and SetPixel() functions. I have rewritten the code to avoid the use of those, but this have to be done in "usafe" context. This means that you have to compile with the compiler option "unsafe", and that the executable will only run on the processor type you compiled it on (afair). You can just replace the two functions below in the GifDecoder.cs file.
Here is the code for the GetPixels function
int [] GetPixels( Bitmap bitmap )
{
	int [] pixels = new int [ 3 * image.Width * image.Height ];
	int count = 0;
    int tw, th;

    BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
    int scanwidth = data.Stride;
    System.IntPtr Scan = data.Scan0;
    int width = bitmap.Width;
    int height = bitmap.Height;

    unsafe
    {
        int offset; // excess pixels at each row, to counter word alignment
        if (bitmap.PixelFormat == PixelFormat.Format32bppArgb ||
            bitmap.PixelFormat == PixelFormat.Format32bppPArgb ||
            bitmap.PixelFormat == PixelFormat.Format32bppRgb)
        {
            offset = scanwidth - bitmap.Width * 4;
        }
        else // assume 24 bits per pixel
            offset = scanwidth - bitmap.Width * 3; 
        int x, y;
        byte colr, colg, colb;
        byte* p = (byte*)(void*)Scan;
        for (y = 0; y < height; y++)
        {
            for (x = 0; x < width; x++)
            {
                pixels[count++] = (int)*(p++);
                pixels[count++] = (int)*(p++);
                pixels[count++] = (int)*(p++);
            }
            p += offset;
        }
    } // End of unsafe
    bitmap.UnlockBits(data);
	return pixels;
}

Here is the code for the SetPixels function
void SetPixels( int [] pixels )
{
	int count = 0;

    BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
        ImageLockMode.ReadWrite, bitmap.PixelFormat);
    int scanwidth = data.Stride;
    System.IntPtr Scan = data.Scan0;
    int width = image.Width;
    int height = image.Height;
    unsafe
    {
        bool fourBytePerPixel = false;
        int offset; // excess pixels at each row, to counter word alignment
        if (bitmap.PixelFormat == PixelFormat.Format32bppArgb ||
            bitmap.PixelFormat == PixelFormat.Format32bppPArgb ||
            bitmap.PixelFormat == PixelFormat.Format32bppRgb)
        {
            offset = scanwidth - bitmap.Width * 4;
            fourBytePerPixel = true;
        }
        else // assume 24 bits per pixel
            offset = scanwidth - bitmap.Width * 3;
        int x, y;
        byte* p = (byte*)(void*)Scan;
        Int32 pixeldata;
        for (y = 0; y < height; y++)
        {
            for (x = 0; x < width; x++)
            {
                pixeldata = pixels[count++];

                *(p++) = (byte)(pixeldata);
                *(p++) = (byte)(pixeldata >> 8);
                *(p++) = (byte)(pixeldata >> 16);
                if (fourBytePerPixel)
                    *(p++) = (byte)(pixeldata >> 24);
            }
            p += offset;
        }
    } // End of unsafe
    bitmap.UnlockBits(data);
}


You must have me excused if the code is a bit cluttered, but it should be usefull anyway. Actually I have not gone into details to see whether it is necessary to test for 24 or 32 bit images, as the code might use purly 32 bit images.

- Lothver

GeneralRe: Has anybody been able to optimize this code? Pin
kitkatrobins24-Sep-07 3:32
memberkitkatrobins24-Sep-07 3:32 
QuestionRe: Has anybody been able to optimize this code? Pin
Ephoy14-Oct-07 7:22
memberEphoy14-Oct-07 7:22 
AnswerRe: Has anybody been able to optimize this code? Pin
kitkatrobins14-Oct-07 23:04
memberkitkatrobins14-Oct-07 23:04 
GeneralRe: Has anybody been able to optimize this code? Pin
TODarkone18-Nov-07 9:15
memberTODarkone18-Nov-07 9:15 
QuestionPlay a gif in a Form? Pin
anderslundsgard22-Mar-07 21:41
memberanderslundsgard22-Mar-07 21:41 
AnswerRe: Play a gif in a Form? Pin
yuxuetaoxp8-Jan-08 3:21
memberyuxuetaoxp8-Jan-08 3:21 
GeneralLZW Patent No. 4,558,302 Pin
Eric Paul Schneider9-Mar-07 17:53
memberEric Paul Schneider9-Mar-07 17:53 
GeneralRe: LZW Patent No. 4,558,302 Pin
Vlasta_20-Mar-07 1:59
memberVlasta_20-Mar-07 1:59 
GeneralRe: LZW Patent No. 4,558,302 Pin
Mons00n22-Mar-07 10:14
memberMons00n22-Mar-07 10:14 
GeneralBtw, this is awesome Pin
dB.19-Feb-07 12:12
memberdB.19-Feb-07 12:12 
GeneralBug: loopcount should be initialized at -1 Pin
dB.19-Feb-07 11:36
memberdB.19-Feb-07 11:36 
GeneralResizing an animated GIF Pin
dB.19-Feb-07 11:32
memberdB.19-Feb-07 11:32 
GeneralAccess to the path "c:\test.gif" is denied. Pin
saikatchoudhury20-Dec-06 20:43
membersaikatchoudhury20-Dec-06 20:43 
QuestionRelease as open source project? Pin
jongalloway16-Aug-06 11:33
memberjongalloway16-Aug-06 11:33 
General[Message Deleted] Pin
Damian Wood2-Jul-06 3:35
memberDamian Wood2-Jul-06 3:35 
GeneralRe: Solution for last 2 pixels of a frame being white Pin
BenBearChen24-Aug-06 18:10
memberBenBearChen24-Aug-06 18:10 
GeneralAnother Solution ! be tested! Pin
tianlupan224-Aug-06 21:31
membertianlupan224-Aug-06 21:31 

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.

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