实例介绍
【实例简介】
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinForGIFSicle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
#region 拖放文件
private void Form1_DragDrop(object sender, DragEventArgs e)
{
DragFile(e);
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
DragFile(e);
}
void DragFile(DragEventArgs e)
{
//如果拖进来的是文件类型
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] paths = e.Data.GetData(DataFormats.FileDrop) as string[];
StringBuilder sb = new StringBuilder();
foreach (var psthStr in paths)
{
sb.Append(psthStr "\r\n");
}
txtFilePath.Text = sb.ToString();
//得到拖进来的路径,取第一个文件
string path = paths[0];
//Clipboard.SetText(path);
//txtFilePath.Text = path;
//string fileName = txtFilePath.Text.Substring(txtFilePath.Text.LastIndexOf('\\') 1);
//路径字符串长度不为空
if (path.Length > 1)
{
//判断是文件夹吗
FileInfo fil = new FileInfo(path);
if (fil.Attributes == FileAttributes.Directory)//文件夹
{
//鼠标图标链接
e.Effect = DragDropEffects.Link;
}
else//文件
{
//鼠标图标链接
e.Effect = DragDropEffects.Link;
}
}
else
{
//鼠标图标禁止
e.Effect = DragDropEffects.None;
}
}
}
#endregion
private void btnSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "GIF文件|*.gif|所有文件|*.*";
ofd.RestoreDirectory = true;
ofd.FilterIndex = 1;
if (ofd.ShowDialog() == DialogResult.OK)
{
string fName = ofd.FileName;
txtFilePath.Text = fName;
//btnLoadImgInfo_Click(null, null);
}
}
private void btnAnBiLiSuoXiao_Click(object sender, EventArgs e)
{
if (!(!string.IsNullOrEmpty(txtFilePath.Text)))
{ MessageBox.Show("请选择文件!"); }
//gifsicle.exe 0.gif --scale 0.8 -o small.gif
string[] filePathArr = txtFilePath.Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var filePath in filePathArr)
{
if (string.IsNullOrEmpty(filePath)) continue;
//string filePath = txtFilePath.Text;
int biLi = int.Parse(txtBiLi.Text);
string str = "gifsicle.exe " filePath " --scale " ((double)biLi / (double)100).ToString("0.00") " -o " filePath.Insert(filePath.Length - 5, DateTime.Now.ToString("yyyyMMddHHmmssfff"));
if (radioButton2.Checked)
{
str = "gifsicle.exe " filePath " --scale " ((double)biLi / (double)100).ToString("0.00") " -o " Environment.GetFolderPath(Environment.SpecialFolder.Desktop) @"/" Path.GetFileName(filePath).Replace(".", DateTime.Now.ToString("yyMMddHHmmssfff") ".");
}
CMD(str);
}
}
private void btnYaSuo_Click(object sender, EventArgs e)
{
if (!(!string.IsNullOrEmpty(txtFilePath.Text)))
{ MessageBox.Show("请选择文件!"); }
//Directory.GetFiles(
//gifsicle -O3 0.gif -o new.gif
//string fileName = txtFilePath.Text.Substring(txtFilePath.Text.LastIndexOf('\\') 1);
string[] filePathArr = txtFilePath.Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var filePath in filePathArr)
{
//string filePath = txtFilePath.Text;
string str = "gifsicle.exe -O3 " filePath " -o " filePath.Insert(filePath.Length - 5, DateTime.Now.ToString("yyyyMMddHHmmssfff"));
if (radioButton2.Checked)
{
str = "gifsicle.exe -O3 " filePath " -o " Environment.GetFolderPath(Environment.SpecialFolder.Desktop) @"/" Path.GetFileName(filePath).Replace(".", DateTime.Now.ToString("yyMMddHHmmssfff") ".");
}
CMD(str);
}
}
string CMD(string mingLing)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
p.Start();//启动程序
//向cmd窗口发送输入信息
p.StandardInput.WriteLine(mingLing "&exit");
p.StandardInput.AutoFlush = true;
//p.StandardInput.WriteLine("exit");
//向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
//同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
//获取cmd窗口的输出信息
string output = p.StandardOutput.ReadToEnd();
//StreamReader reader = p.StandardOutput;
//string line=reader.ReadLine();
//while (!reader.EndOfStream)
//{
// str = line " ";
// line = reader.ReadLine();
//}
p.WaitForExit();//等待程序执行完退出进程
p.Close();
//Console.WriteLine(output);
return output;
}
private void txtBiLi_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= (char)48 && e.KeyChar <= (char)57) || e.KeyChar == (char)Keys.Delete || e.KeyChar == (char)Keys.Back)
{
}
else
{
e.Handled = true;
}
}
private void btnClearTxt_Click(object sender, EventArgs e)
{
txtFilePath.Clear();
txtFilePath.ClearUndo();
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论