星期三, 3月 14, 2007

[.Net]How to generate animated gif

Native .Net framework can generate gif, but it cannot generate animated gif.

I spend lots of time to search solution in Google. I almost give up.

Finally I found NGif in SourceForge.net.



After you downloaded, you can directly refer the example code to use. That's easy.

Here I post the part of the example:

    /* create Gif */
    //you should replace filepath
    String [] imageFilePaths = new String[]{"G:\\01.png"};
    String outputFilePath = "G:\\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();



NGif also provide the decode method, so you can save every frame in gif to separated image.

沒有留言: