zoukankan      html  css  js  c++  java
  • 动态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 );
            }
        }
    }
  • 相关阅读:
    初学:利用mybatis-generator自动生成代码
    数组求和forEach方法
    Node.js热部署代码,实现修改代码后自动重启服务方便实时调试
    nodejs中文乱码问题
    简单说一下SS的原理
    Spring Cloud Eureka 3 (Eureka client注册服务提供者)
    Spring Cloud Eureka 2 (Eureka Server搭建服务注册中心)
    Spring Cloud Eureka 1(eureka简介)
    eclipse中创建多模块maven web项目
    eclipse中创建maven web项目
  • 原文地址:https://www.cnblogs.com/bober/p/2194108.html
Copyright © 2011-2022 走看看