基本信息
源码名称:gif小工具源码(分解Gif为Jpg,也可以将jpg合并为gif)
源码大小:3.35M
文件格式:.zip
开发语言:C#
更新时间:2019-04-11
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

【使用方法】


分解gif流程:  

  1. 选择文件
  2. 点击分解单选框
  3. 点击确定
  4. 分解成功

合并gif流程:

  1. 点击合成单选框
  2. 是否重复(可选)
  3. 是否反转(可选,可用于动图倒放)
  4. 输入每帧延时(可空,默认50)
  5. 点击确定
  6. 选择图片(多张)
  7. 合成成功



using Gif.Components;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
	public class Form1 : Form
	{
		private delegate void SetProgressBarCallBack(int val);

		private delegate void SetPictureBoxCallBack(Image img);

		private int delay;

		private List<string> pathes;

		private AnimatedGifEncoder animated;

		private IContainer components;

		private TextBox tbPath;

		private Button btnOpen;

		private ProgressBar progressBar1;

		private TextBox tbDelay;

		private Label label1;

		private Button btnStart;

		private Panel panel1;

		private RadioButton rbCompose;

		private RadioButton rbSpilt;

		private CheckBox cbIsReverse;

		private CheckBox cbIsRepeat;

		private PictureBox pbNew;

		public Form1()
		{
			this.InitializeComponent();
			if (!Directory.Exists("分解"))
			{
				Directory.CreateDirectory("分解");
			}
			if (!Directory.Exists("gifs"))
			{
				Directory.CreateDirectory("gifs");
			}
		}

		private void BtnOpen_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.DefaultExt = ".gif";
			openFileDialog.Filter = "gif file|*.gif";
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				this.tbPath.Text = openFileDialog.FileName;
				this.pbNew.Image = Image.FromFile(openFileDialog.FileName);
			}
		}

		private void BtnStart_Click(object sender, EventArgs e)
		{
			if (this.rbSpilt.Checked)
			{
				this.Split();
			}
			else
			{
				this.Compose();
			}
		}

		private void Split()
		{
			if (!Directory.Exists("分解"))
			{
				Directory.CreateDirectory("分解");
			}
			if (string.IsNullOrEmpty(this.tbPath.Text))
			{
				MessageBox.Show("请先选择一张gif图", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			}
			else
			{
				Image image = Image.FromFile(this.tbPath.Text);
				FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
				int frameCount = image.GetFrameCount(dimension);
				FileInfo fileInfo = new FileInfo(this.tbPath.Text);
				string text = "分解\\"   fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
				this.CreateDir(text);
				this.ClearDir(text);
				for (int i = 0; i < frameCount; i  )
				{
					image.SelectActiveFrame(dimension, i);
					image.Save(text   "\\"   i   ".jpg", ImageFormat.Jpeg);
				}
				MessageBox.Show("分解成功!文件保存至"   Application.StartupPath   "\\"   text, "成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
		}

		private void Compose()
		{
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Expected O, but got Unknown
			if (!Directory.Exists("gifs"))
			{
				Directory.CreateDirectory("gifs");
			}
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.DefaultExt = ".jpg";
			openFileDialog.Filter = "jpg file|*.jpg";
			openFileDialog.Multiselect = true;
			openFileDialog.InitialDirectory = Application.StartupPath   "\\分解";
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				this.animated = new AnimatedGifEncoder();
				try
				{
					this.delay = int.Parse(this.tbDelay.Text);
				}
				catch (Exception)
				{
					this.delay = 50;
					this.tbDelay.Text = 50.ToString();
				}
				this.animated.SetDelay(this.delay);
				if (this.cbIsRepeat.Checked)
				{
					this.animated.SetRepeat(0);
				}
				else
				{
					this.animated.SetRepeat(-1);
				}
				this.pathes = new List<string>();
				this.pathes.AddRange(openFileDialog.FileNames);
				Thread thread = new Thread(this.ThreadCompose);
				thread.IsBackground = true;
				thread.Start(null);
			}
		}

		private void ThreadCompose(object o)
		{
			string text = "gifs\\"   DateTime.Now.Ticks.ToString()   ".gif";
			this.animated.Start(text);
			double num = (double)(100 / this.pathes.Count);
			int num2 = 1;
			foreach (string pathe in this.pathes)
			{
				this.animated.AddFrame(Image.FromFile(pathe));
				this.SetProgressBar((int)(num * (double)num2));
				num2  ;
			}
			this.animated.Finish();
			this.SetProgressBar(100);
			this.SetPictureBox(Image.FromFile(text));
			MessageBox.Show("合成成功,文件保存至"   Application.StartupPath   "\\"   text, "成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}

		private void CreateDir(string path)
		{
			if (Directory.Exists(path))
			{
				if (MessageBox.Show("已经存在同名文件夹,是否覆盖", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.Cancel)
				{
					return;
				}
				new DirectoryInfo(path).Delete(true);
			}
			Directory.CreateDirectory(path);
		}

		private void ClearDir(string path)
		{
			if (Directory.Exists(path))
			{
				FileSystemInfo[] fileSystemInfos = new DirectoryInfo(path).GetFileSystemInfos();
				foreach (FileSystemInfo fileSystemInfo in fileSystemInfos)
				{
					if (fileSystemInfo is DirectoryInfo)
					{
						new DirectoryInfo(fileSystemInfo.FullName).Delete(true);
					}
					else
					{
						File.Delete(fileSystemInfo.FullName);
					}
				}
			}
		}

		private void SetProgressBar(int val)
		{
			if (this.progressBar1.InvokeRequired)
			{
				base.Invoke(new SetProgressBarCallBack(this.SetProgressBar), val);
			}
			else
			{
				this.progressBar1.Value = val;
			}
		}

		private void SetPictureBox(Image img)
		{
			if (this.pbNew.InvokeRequired)
			{
				base.Invoke(new SetPictureBoxCallBack(this.SetPictureBox), img);
			}
			else
			{
				this.pbNew.Image = img;
			}
		}

		private void Form1_DragDrop(object sender, DragEventArgs e)
		{
			string text = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
			if (text.EndsWith("gif"))
			{
				this.tbPath.Text = text;
				this.pbNew.Image = Image.FromFile(this.tbPath.Text);
			}
			else
			{
				MessageBox.Show("请拖入gif文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			}
		}

		private void Form1_DragEnter(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop))
			{
				e.Effect = DragDropEffects.All;
			}
			else
			{
				e.Effect = DragDropEffects.None;
			}
		}