实例介绍
【实例简介】
【使用方法】
分解gif流程:
- 选择文件
- 点击分解单选框
- 点击确定
- 分解成功
合并gif流程:
- 点击合成单选框
- 是否重复(可选)
- 是否反转(可选,可用于动图倒放)
- 输入每帧延时(可空,默认50)
- 点击确定
- 选择图片(多张)
- 合成成功
【实例截图】
【核心代码】
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; } }
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论