原生的 .Net framework 雖然可以產生 gif 圖檔,但卻無法產生有動畫效果的 gif 圖檔。
找了半天,本來已經萬念俱灰,想說可能要自己來寫了。
但終於還是在SourceForge.net找到合用的library:NGif。
下載以後,可以直接參考程式,使用方法很簡單~
這邊摘錄製作的方法:
/* create Gif */
//you should replace filepath
String [] imageFilePaths = new String[]{"G:\1.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同時也提供解開的方法,所以你也可以將 gif 裡面的每個 frame 都個別存為獨立的圖檔。