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
