dll下載的地址
使用主要調用的是Components這個代碼庫,引用dll就可以了。
作者:
以下為這條連接的博客園內容:
思路很簡單,就是先把gif分解成很多幀,然后對幀進行合並,合並幀之前把幀的位置反轉一下就可以了。因為我自己對圖像處理的知識不懂,只想到了思路,所以這些功能都要找些資料,然后修改,測試。
分割幀的代碼如下

//解碼gif圖片 public List<string> GetFrames(stringpPath,stringpSavedPath) { Image gif=Image.FromFile(pPath); FrameDimension fd=newFrameDimension(gif.FrameDimensionsList[0]); //獲取幀數(gif圖片可能包含多幀,其它格式圖片一般僅一幀) intcount=gif.GetFrameCount(fd); List<string>gifList=newList<string>(); //以Jpeg格式保存各幀 for(inti=0;i<count;i++) { gif.SelectActiveFrame(fd,i); gif.Save(pSavedPath+"\\frame_"+i+".png",ImageFormat.Png); gifList.Add(pSavedPath+"\\frame_"+i+".png"); } returngifList; }

可以看到,返回了一個包含所有生成的幀地址的list列表。然后就是使用gifList作為參數來合並了。

//獲取系統臨時目錄存放解碼后的png圖片\r\n stringtemppath=System.Environment.GetEnvironmentVariable("TEMP"); List<string>gifList=GetFrames(tBoxFile.Text,temppath); gifList.Reverse(); StringoutputFilePath="new.gif"; AnimatedGifEncoder ae=newAnimatedGifEncoder(); ae.Start(outputFilePath); ae.SetDelay(100); // 延遲間隔\r\n ae.SetRepeat(0); //-1:不循環,0:總是循環 播放 \r\n for(inti=0,count=gifList.Count;i<count;i++) { ae.AddFrame(Image.FromFile(gifList[i])); } ae.Finish(); MessageBox.Show("成功!新文件已保存在同目錄");

這里面使用了AnimatedGifEncoder這個類,這是Gif.Components.dll動態連接庫里的類(此庫開源,文末給出地址),是我在codeProject上找到的。首先我把gifList反轉,然后合並保存到同目錄。中間生成的幀為了方便我保存到了temp目錄。
本來這個庫里是分割gif的功能的。但是我實際測試后發現效果非常差,圖片黑條泛濫,根本沒法看。所以還是使用上面那段代碼,相關代碼我依然保存在工程里,有興趣可以自己測試。