动态GIF图片处理

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
 
public class ImageHelper
{
    /// <summary>
    
/// 获取图片中的各帧
    
/// </summary>
    
/// <param name="pPath">图片路径</param>
    
/// <param name="pSavePath">保存路径</param>
    public void GetFrames(string pPath, string pSavedPath)
    {
        Image gif = Image.FromFile(pPath);
        FrameDimension fd = new FrameDimension(gif.FrameDimensionsList[0]);
        
        //获取帧数(gif图片可能包含多帧,其它格式图片一般仅一帧)
        int count = gif.GetFrameCount(fd);
        
        //以Jpeg格式保存各帧
        for (int i = 0; i < count; i++)
        {
            gif.SelectActiveFrame(fd, i);
            gif.Save(pSavedPath + "\\frame_" + i + ".jpg", ImageFormat.Jpeg);
        }
    }
    //http://www.codeproject.com/KB/GDI-plus/NGif.aspx
    public bool CreateGifImage()
    {
        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 );
        }
    }
}
posted @ 2011-09-28 13:02  Care健康  阅读(603)  评论(0编辑  收藏  举报
版权
作者:Bober Song

出处:http://bober.cnblogs.com

Care健康:http://www.aicareyou.com

推荐空间:华夏名网

本文首发博客园,版权归作者跟博客园共有。

转载必须保留本段声明,并在页面显著位置给出本文链接,否则保留追究法律责任的权利。