续上集.关于gif图片的分解与组合

 

一个Gif图像文件,是有几个文件进行合成的,因此处理此类文件的时候,不能像Jpeg或者Bmp文件那样处理。需要把Gif文件拆分帧的形式,然后对每一帧进行处理,处理完后再合成Gif

 

其实网上有个例子对于Gif处理非常详细,地址如下。

http://www.codeproject.com/dotnet/NGif.asp

 

但是对于一个Gif进行拆分,其实Image对象本身就支持,例如对于一个Gif文件拆分成Jpeg文件方式,可以按照如下的方式进行处理。

    using System.Drawing.Drawing2D;

    using System.Drawing.Imaging;

 

    Image imgGif = Image.FromFile(@"d:/test.gif");

    //Create a new FrameDimension object from this image

    FrameDimension ImgFrmDim = new FrameDimension( imgGif.FrameDimensionsList[0] );

           

    //Determine the number of frames in the image

    //Note that all images contain at least 1 frame,

    //but an animated GIF will contain more than 1 frame.

    int nFrameCount = imgGif.GetFrameCount( ImgFrmDim );

 

    // Save every frame into jpeg format

    for( int i = 0; i < nFrameCount; i++ )

    {

        imgGif.SelectActiveFrame( ImgFrmDim, i );

        imgGif.Save( string.Format( @"d:/Frame{0}.jpg", i ), ImageFormat.Jpeg );

    }

 

 

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值