当年上学时候写的一个很简单的东西,拖动图片到程序中自动重命名保存到指定位置,
原图文地址:http://user.qzone.qq.com/652768664/blog/1363151915
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Threading;
namespace DragPhoto
{
///
///
/// 关于: 这个是拖动本地图片,批量操作或者多线程下载来自网络的图片的小工具
///来由:保存空间图片很费劲,比较麻烦,直接拖拽图片比较好
/// 作者:intbird;网易邮箱:intbird@163.com ; QQ652768664
///source_time:2013-3-12;
///
///
public partial classForm1 : Form
{
private FolderBrowserDialog dia = newFolderBrowserDialog();
//用于存储文件的键值对,键:文件全路径;值:文件地址;
private Dictionary dic = new Dictionary();
//获取保存文件的文件夹地址
private String savePath = null;
private String localPath = null;
//下载文件名
private string DragFile = "";
//获取的文件信息
private FileInfo file = null;
//报告异常的 委托方法
private delegate void myAdd(Object ee);
private myAdd myadd;
//下载计数,用于浏览图片
private static int downCount = 0;
public Form1()
{
InitializeComponent();
}
private void init()
{
dic.Clear();
lsbDragtarget.Items.Clear();
downCount= 0;
lsbDragtarget.Items.Clear();
dia = newFolderBrowserDialog();
}
///
/// 将选择的文件夹显示在该项下面
///
///
///
private void savechooserMenuItem_Click(objectsender, EventArgs e)
{
init();
if(dia.ShowDialog() == DialogResult.OK)
{
ToolStripMenuItemshowPathItem =
new ToolStripMenuItem("保存位置在{ " +dia.SelectedPath + "点击打开目录 }");
savePath =dia.SelectedPath;
labResult.Text = "文件保存位置 :" +savePath;
saveMenu.DropDownItems.Add(showPathItem);
showPathItem.Click += newEventHandler(openDirectory);
}
dia =null;
}
#region 拖放文件操作
///
/// 当拖动文件进入列表框时,将收到的数据存放入字典,将字典的值填充列表项
///
///
///
private void lsbSource_DragEnter(object sender,DragEventArgs e)
{
//BackgroundWorker work = new BackgroundWorker();
//work.DoWork += new DoWorkEventHandler(Dowork);
getDragFile(e);
}
///
/// 判断拖动的数据
///
///
private void getDragFile(DragEventArgs e)
{
//如果拖入的数据是文件或者是文件地址,将URL存入键
if((e.Data.GetDataPresent(DataFormats.FileDrop)
||e.Data.GetDataPresent(DataFormats.Text))
&& savePath !=null)
{
//获取得到的字符串数组 或 文本
string[] dragFiles =(string[])e.Data.GetData(DataFormats.FileDrop);
String dragFile =(string)e.Data.GetData(DataFormats.Text);
//如果没有获取到任何数据,返回
if ((dragFile == null)
&& (dragFiles == null))
{
labResult.Text = "没有获取到数据!";
return;
}
//如果是文件,并且获取到了文件列表,遍历
if (dragFiles != null)
{
foreach (string fileName in dragFiles)
{
file = newFileInfo(fileName);
saveLocalPic(file);
}
}
//如果获取到的网络文图片地址,开启下载线程
if (dragFile != null)
{
DragFile = dragFile;
Thread thread = new Thread(saveURLPic);
thread.Start();
return;
}
}
else
{
labResult.ForeColor =Color.Red;
labResult.Text = "失败! 是否已经选择了文件保存位置\n 或者地址不正确!";
}
}
///
/// 判断本地拖拽的是否是图片文件,如果是,则保存
///
///
private void saveLocalPic(FileInfo file)
{
try
{
if(isJpg(file.Extension))
{
file.CopyTo(savePath + "\\" +currentDateString() + file.Name, false);
}
}
catch(Exception error)
{
labResult.Text =error.Message;
return;
}
return;
}
///
/// 下载网络图片
///
private void saveURLPic()
{
myadd =new myAdd(add);
try
{
WebClient client = newWebClient();
//文件名
string webFileName = savePath+ currentDateString() + ".jpg";
//通知列表框添加数据
lsbDragtarget.Invoke(myadd,webFileName);
dic.Add(downCount,webFileName);
downCount++;
//开始下载
client.DownloadFile(DragFile,webFileName);
client.Dispose();
}
catch(Exception ee)
{
//捕获异常
//labResult.Invoke(myadd,ee);
MessageBox.Show(ee.Message +"\n确定拖动的是图片?");
}
}
private void add(Object e)
{
labResult.Text = "开始下载:"+e.ToString();
lsbDragtarget.Items.Add(e.ToString());
}
#endregion
#region 本地文件遍历
///
/// 将选择的本地文件夹显示在该项下面
///
///
///
private void localchooserMenuItem_Click(objectsender, EventArgs e)
{
init();
if(dia.ShowDialog() == DialogResult.OK)
{
ToolStripMenuItemshowPathItem =
new ToolStripMenuItem("所选目录为{ " +dia.SelectedPath + "点击打开目录 }");
localPath =dia.SelectedPath;
labResult.Text = "遍历目录 :" +localPath;
lstFilesInDir(Directory.CreateDirectory(dia.SelectedPath));
localMenu.DropDownItems.Add(showPathItem);
showPathItem.Click += newEventHandler(openDirectory);
}
dia =null;
}
///
///
///
///
///
public void lstFilesInDir(FileSystemInfoinfo)
{
DirectoryInfo dir = info as DirectoryInfo;
if(info.Exists && dir != null)
{
FileSystemInfo[] files =dir.GetFileSystemInfos();
for (int i = 0; i <files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
if (file != null)
{
lsbDragtarget.Items.Add(file.Name);
dic.Add(downCount,file.FullName);
downCount++;
}
else
lstFilesInDir(files[i]);
}
}
}
#endregion
#region 共用事件和方法
///
/// 打开(保存或者本地)目录操作
///
///
///
private void openDirectory(object obj, EventArgse)
{
ToolStripDropDownItem tool = (ToolStripDropDownItem)obj;
if(tool.OwnerItem.Name == "saveMenu")
{
Process.Start("Explorer.exe", savePath);
}
if(tool.OwnerItem.Name == "localMenu")
{
Process.Start("Explorer.exe", localPath);
}
}
///
/// 判断字符串后缀名是不是已知的图片格式
///
/// 字符串
/// 如果是图片.返回真
private bool isJpg(string fileName)
{
if(fileName != null
&&(fileName.ToLower().EndsWith(".jpg")
||fileName.ToLower().EndsWith(".jpeg")
||fileName.ToLower().EndsWith(".png")
||fileName.ToLower().EndsWith(".gif")
||fileName.ToLower().EndsWith(".bmp")))
return true;
else
return false;
}
///
/// 将当前的时间以 月_天_时_分_秒_毫秒_ 返回
///
/// 返回字符串
private string currentDateString()
{
stringdatetime = DateTime.Now.Month.ToString()
+DateTime.Now.Day.ToString() + "_"
+DateTime.Now.Hour.ToString() + "_"
+DateTime.Now.Minute.ToString() + "_"
+DateTime.Now.Second.ToString() + "_"
+DateTime.Now.Millisecond.ToString() + "_";
returndatetime;
}
#endregion
///
/// 键盘 See 预览图片
///
///
///
private void lsbDragtarget_KeyPress(objectsender, KeyPressEventArgs e)
{
if(e.KeyChar==(char)Keys.S
&&lsbDragtarget.Items!=null
&&lsbDragtarget.SelectedIndex>=0
&&dic!=null)
{
intindex=lsbDragtarget.SelectedIndex;
if ( index!=lsbDragtarget.Items.Count-1)
{
try
{
//图片路径
Stringvalue = dic[index].ToString();
picView.Image = Image.FromFile(value);
labResult.Text = "see pictrue...";
}
catch (Exception ex)
{
labResult.Text = "图片未完成下载,或者拖入的并非图片格式......";
}
lsbDragtarget.SelectedIndex++;
}
}
else if(e.KeyChar == (char)Keys.D
&& lsbDragtarget.Items != null
&& lsbDragtarget.SelectedIndex >=0
&& dic != null)
{
int index =lsbDragtarget.SelectedIndex;
try
{
String value = dic[index].ToString();
picView.Image = null;
File.Delete(value);
lsbDragtarget.Items.RemoveAt(index);
dic.Remove(index);
}
catch (Exception ex)
{
labResult.Text = ex.Message;
}
}
}
private void aboatMenuItem_Click(object sender,EventArgs e)
{
MessageBox.Show("作者QQ652768664;");
}
private void DescriptionMenuItem_Click(objectsender, EventArgs e)
{
Process.Start("http://652768664.qzone.qq.com");
}
}
}