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 );
            }
        }
    }
  • 相关阅读:
    ST的电机开发硬件套件NUCLEO-G431RB和X-NUCLEO-IHM16M1
    计算机软件世界的思维方式
    vue 动态路由配置,路由由后台返回
    element Cascader组件通过最后一级ID查找匹配的父级ID
    git push本地代码成功,远程提交未更新
    面试整理之原理问题
    JS执行环境与作用域理解
    webpack原理类型问题
    __webpack_require__(...) is not a function
    express+cors模拟解决跨域
  • 原文地址:https://www.cnblogs.com/bober/p/2194108.html
Copyright © 2011-2022 走看看