Gift动图分解小工具

gif 动图分解小工具

Overview

因为自己有时候需要将一些gif图片分解,但是没有在网上找到合适的工具,所有就自己写了一个,在这里与大家分享,其实实现很简单,是通过C#实现的。文章下方有下载链接!

效果图

分解的图片为:

分解后

实现代码

/*
 GIF分解小工具
 作者 鲁迅认识的那只猹
 联系方式: 1258730808@qq.com
 创建时间: 2017-8-14 09:59:28
 编辑历史:
 2018年3月13日14:56:40 将jpg的导出格式改为了png的导出格式
 */
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace GIFTools
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
         
            //禁止改变窗体大小
            this.MaximumSize = this.MinimumSize;
            //取消跨线程的检查
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        /// <summary>
        /// 选择要进行分解的gif图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGifBorrow_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Filter = "GIF图片|*.gif";
                dialog.Multiselect = false;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string path = dialog.FileName;
                    tbGifPath.Text = path;
                }
            }
        }

        /// <summary>
        /// 选择导出图片的文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExportBorrow_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog dialog = new FolderBrowserDialog())
            {
                dialog.ShowNewFolderButton = true;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string path = dialog.SelectedPath;
                    this.tbExportPath.Text = path;
                }
            }
        }

        /// <summary>
        /// 导出文件按钮的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            string gifPath = tbGifPath.Text.Trim();
            string folderPaht = tbExportPath.Text.Trim();
            if (string.IsNullOrEmpty(gifPath) || string.IsNullOrEmpty(folderPaht))
            {
                MessageBox.Show("请选择gif路径或者导出路径!");
                return;
            }

            if (btnExport.Text == "导出文件")
            {
                btnExport.Text = "导出中...";
                //开启线程导出图片
                Thread thread = new Thread(Export);
                thread.Start();
            }
            else
            {
                MessageBox.Show("正在导出中!");
                return;
            }

        }

        /// <summary>
        /// 导出jpg图片
        /// </summary>
        private void Export()
        {
            string gifPath = tbGifPath.Text.Trim();
            string folderPaht = tbExportPath.Text.Trim();

            Image img = Image.FromFile(gifPath);
            FrameDimension fd = new FrameDimension(img.FrameDimensionsList[0]);
            //获取gif帧的数量
            int count = img.GetFrameCount(fd);

            //遍历保存图片
            for (int i = 0; i < count; i++)
            {
                img.SelectActiveFrame(fd, i);
                string imgPath = folderPaht + "\frame" + (i + 1) + ".png";
                //判断同名文件是否存在
                if (File.Exists(imgPath))
                {
                    File.Delete(imgPath);
                }
                //保存图片 一定要设置格式 否则保存出来的图片都是一张图片
                img.Save(imgPath, ImageFormat.Png);
            }

            MessageBox.Show("文件导出成功!");
            btnExport.Text = "导出文件";
        }
    }
}

源码下载

下面是下载链接,其中也包括了可执行的文件
https://files.cnblogs.com/files/slyfox/GIF分解工具.rar

原文地址:https://www.cnblogs.com/slyfox/p/8557050.html