65.9K
CodeProject 正在变化。 阅读更多。
Home

NGif,.NET 动画 GIF 编码器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.02/5 (58投票s)

2005年9月1日

CPOL
viewsIcon

1535631

downloadIcon

18055

使用 C# 创建动画 GIF 图像。

Sample Image - NGif.gif

引言

由于 .NET Framework 无法创建动画 GIF 图像,NGif 提供了一种在 .NET 框架中创建 GIF 动画的方式。它可以从多个图像创建动画 GIF,并从动画 GIF 中提取图像。

使用代码

/* 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 );
}

关注点

在写入固定字节结构的二进制文件时,使用 Stream 替换 BinaryWriter

历史

  • 2005年8月31日:草稿。
© . All rights reserved.