111,094
社区成员




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Threading;
namespace WindowsTest
{
public partial class entry : Form
{
public entry()
{
InitializeComponent();
}
#region 按钮事件
/// <summary>
/// 退出应用程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonExit_Click(object sender, EventArgs e)
{
Application.Exit();//退出应用程序
}
/// <summary>
/// 选择原路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSearchOld_Click(object sender, EventArgs e)
{
this.folderBrowserDialogOldPatch.ShowNewFolderButton = true;
this.folderBrowserDialogOldPatch.ShowDialog();
this.textBoxOldPath.Text = this.folderBrowserDialogOldPatch.SelectedPath;
}
/// <summary>
/// 选择新路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSearchNew_Click(object sender, EventArgs e)
{
this.folderBrowserDialogOldPatch.ShowNewFolderButton = true;
this.folderBrowserDialogNewPath.ShowDialog();
this.textBoxNewPath.Text = this.folderBrowserDialogNewPath.SelectedPath;
}
/// <summary>
/// 开始监控原文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAutoWatcher_Click(object sender, EventArgs e)
{
if (this.textBoxNewPath.Text.Trim() == string.Empty)
{
MessageBox.Show("请选择目标路径", "错误:目标路径为空", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (this.textBoxOldPath.Text.Trim() == string.Empty)
{
MessageBox.Show("请选择原路径", "错误:原路径为空", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (this.buttonAutoWatcher.Text == "自动")
{
this.buttonAutoWatcher.Text = "停止";
this.SetEnable(false);
this.StartWatcher();
}
else
{
this.buttonAutoWatcher.Text = "自动";
this.SetEnable(true);
}
}
#endregion
#region 自定义方法
/// <summary>
/// 设定按钮的是否可用
/// </summary>
/// <param name="s">是否可用</param>
private void SetEnable(bool s)
{
this.buttonSearchNew.Enabled = s;
this.buttonSearchOld.Enabled = s;
this.textBoxNewPath.Enabled = s;
this.textBoxOldPath.Enabled = s;
this.checkBoxIncludeSub.Enabled = s;
}
private void StartWatcher()
{
fw.BeginInit();
fw.IncludeSubdirectories = this.checkBoxIncludeSub.Checked;//是否包含子文件夹
fw.Path = this.textBoxOldPath.Text;
fw.Changed += new System.IO.FileSystemEventHandler(OnChanged);
fw.Created += new System.IO.FileSystemEventHandler(OnCreated);
fw.Renamed += new System.IO.RenamedEventHandler(OnRenamed);
fw.Deleted += new System.IO.FileSystemEventHandler(OnDeleted);
fw.EnableRaisingEvents = true;
fw.EndInit();
}
void OnDeleted(object sender, System.IO.FileSystemEventArgs e)
{
}
void OnRenamed(object sender, System.IO.RenamedEventArgs e)
{
}
void OnCreated(object sender, System.IO.FileSystemEventArgs e)
{
string NewPath = e.FullPath;
NewPath = NewPath.Replace(this.textBoxOldPath.Text.Trim(), this.textBoxNewPath.Text);
this.CreateFile(e.FullPath, NewPath);
}
void OnChanged(object sender, System.IO.FileSystemEventArgs e)
{
}
/// <summary>
/// 生成新的文件
/// </summary>
/// <param name="ol">原图片路径</param>
/// <param name="ne">新图片路径</param>
/// <returns></returns>
private void CreateFile(string ol, string ne)
{
if (File.Exists(ol))
{
if (!Directory.Exists(ne.Substring(0, ne.LastIndexOf('\\'))))
{
Directory.CreateDirectory(ne.Substring(0, ne.LastIndexOf('\\')));
}
string exFileName= ol.Substring(ol.LastIndexOf('.')).ToLower();
if (exFileName == ".jpg" || exFileName == ".gif" || exFileName == ".bmp"||exFileName==".jpeg"||exFileName==".png")
{
this.ShowThumbnail(ol, ne,Convert.ToInt32(numericUpDownHeight.Value),Convert.ToInt32(numericUpDownWidth.Value));
}
}
}
public void ShowThumbnail(string oldfile, string newfile, int h, int w)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(oldfile);
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
int oldh = img.Height;
int oldw = img.Width;
int newh, neww;
double h1 = oldh * 1.0 / h;
double w1 = oldw * 1.0 / w;
double f = (h1 > w1) ? h1 : w1;
if (f < 1.0)
{
newh = oldh;
neww = oldw;
}
else
{
newh = (int)(oldh / f);
neww = (int)(oldw / f);
}
System.Drawing.Image myThumbnail = img.GetThumbnailImage(neww, newh, myCallback, IntPtr.Zero);
string exFileName = oldfile.Substring(oldfile.LastIndexOf('.')+1).ToLower();
switch (exFileName)
{
case "jpg":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "bmp":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case "jpeg":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "gif":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Gif);
break;
case "png":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Png);
break;
default:
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
img.Dispose();
myThumbnail.Dispose();
}
private static bool ThumbnailCallback()
{
return false;
}
#endregion
}
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="oldfile">原文件完整路径</param>
/// <param name="newfile">目标文件完整路径</param>
/// <param name="h">缩放后的最大高度</param>
/// <param name="w">缩放后的最大宽度</param>
/// <returns></returns>
public bool ShowThumbnail(string oldfile, string newfile, int h, int w)
{
lock (lockObj)
{
FileStream fss = new FileStream(oldfile, FileMode.Open);
System.Drawing.Image img = System.Drawing.Image.FromStream(fss);
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
#region 计算高度,宽度
int oldh = img.Height;
int oldw = img.Width;
int newh, neww;
double h1 = oldh * 1.0 / h;
double w1 = oldw * 1.0 / w;
double f = (h1 > w1) ? h1 : w1;
if (f < 1.0)
{
newh = oldh;
neww = oldw;
}
else
{
newh = (int)(oldh / f);
neww = (int)(oldw / f);
}
#endregion
System.Drawing.Image myThumbnail = img.GetThumbnailImage(neww, newh, myCallback, IntPtr.Zero);
string exFileName = oldfile.Substring(oldfile.LastIndexOf('.') + 1).ToLower();
switch (exFileName)
{
case "jpg":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "bmp":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case "jpeg":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "gif":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Gif);
break;
case "png":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Png);
break;
default:
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
fss.Close();
fss.Dispose();
img.Dispose();
myThumbnail.Dispose();
return true;
}
}
private static bool ThumbnailCallback()
{
return false;
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Threading;
namespace WindowsTest
{
public partial class entry : Form
{
private static Object lockObj = new object();
public entry()
{
InitializeComponent();
}
#region 按钮事件
/// <summary>
/// 退出应用程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonExit_Click(object sender, EventArgs e)
{
Application.Exit();//退出应用程序
}
/// <summary>
/// 选择原路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSearchOld_Click(object sender, EventArgs e)
{
this.folderBrowserDialogOldPatch.ShowNewFolderButton = true;
this.folderBrowserDialogOldPatch.ShowDialog();
this.textBoxOldPath.Text = this.folderBrowserDialogOldPatch.SelectedPath;
}
/// <summary>
/// 选择新路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSearchNew_Click(object sender, EventArgs e)
{
this.folderBrowserDialogOldPatch.ShowNewFolderButton = true;
this.folderBrowserDialogNewPath.ShowDialog();
this.textBoxNewPath.Text = this.folderBrowserDialogNewPath.SelectedPath;
}
/// <summary>
/// 开始监控原文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAutoWatcher_Click(object sender, EventArgs e)
{
if (this.textBoxNewPath.Text.Trim() == string.Empty)
{
MessageBox.Show("请选择目标路径", "错误:目标路径为空", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (this.textBoxOldPath.Text.Trim() == string.Empty)
{
MessageBox.Show("请选择原路径", "错误:原路径为空", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (this.buttonAutoWatcher.Text == "自动")
{
this.buttonAutoWatcher.Text = "停止";
this.SetEnable(false);
this.StartWatcher();
}
else
{
this.buttonAutoWatcher.Text = "自动";
this.SetEnable(true);
}
}
#endregion
#region 自定义方法
/// <summary>
/// 设定按钮的是否可用
/// </summary>
/// <param name="s">是否可用</param>
private void SetEnable(bool s)
{
this.buttonSearchNew.Enabled = s;
this.buttonSearchOld.Enabled = s;
this.textBoxNewPath.Enabled = s;
this.textBoxOldPath.Enabled = s;
this.checkBoxIncludeSub.Enabled = s;
}
private void StartWatcher()
{
fw.BeginInit();
fw.IncludeSubdirectories = this.checkBoxIncludeSub.Checked;//是否包含子文件夹
fw.Path = this.textBoxOldPath.Text;
fw.Changed += new System.IO.FileSystemEventHandler(OnChanged);
fw.Created += new System.IO.FileSystemEventHandler(OnCreated);
fw.Renamed += new System.IO.RenamedEventHandler(OnRenamed);
fw.Deleted += new System.IO.FileSystemEventHandler(OnDeleted);
fw.EnableRaisingEvents = true;
fw.EndInit();
}
void OnDeleted(object sender, System.IO.FileSystemEventArgs e)
{
}
void OnRenamed(object sender, System.IO.RenamedEventArgs e)
{
}
void OnCreated(object sender, System.IO.FileSystemEventArgs e)
{
string NewPath = e.FullPath;
NewPath = NewPath.Replace(this.textBoxOldPath.Text.Trim(), this.textBoxNewPath.Text);
this.CreateFile(e.FullPath, NewPath);
}
void OnChanged(object sender, System.IO.FileSystemEventArgs e)
{
}
/// <summary>
/// 生成新的文件
/// </summary>
/// <param name="ol">原图片路径</param>
/// <param name="ne">新图片路径</param>
/// <returns></returns>
private void CreateFile(string ol, string ne)
{
if (File.Exists(ol))
{
if (!Directory.Exists(ne.Substring(0, ne.LastIndexOf('\\'))))
{
Directory.CreateDirectory(ne.Substring(0, ne.LastIndexOf('\\')));
}
string exFileName= ol.Substring(ol.LastIndexOf('.')).ToLower();
if (exFileName == ".jpg" || exFileName == ".gif" || exFileName == ".bmp"||exFileName==".jpeg"||exFileName==".png")
{
this.ShowThumbnail(ol, ne,Convert.ToInt32(numericUpDownHeight.Value),Convert.ToInt32(numericUpDownWidth.Value));
}
}
}
public void ShowThumbnail(string oldfile, string newfile, int h, int w)
{
lock (lockObj)
{ System.Drawing.Image img = System.Drawing.Image.FromFile(oldfile);
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
int oldh = img.Height;
int oldw = img.Width;
int newh, neww;
double h1 = oldh * 1.0 / h;
double w1 = oldw * 1.0 / w;
double f = (h1 > w1) ? h1 : w1;
if (f < 1.0)
{
newh = oldh;
neww = oldw;
}
else
{
newh = (int)(oldh / f);
neww = (int)(oldw / f);
}
System.Drawing.Image myThumbnail = img.GetThumbnailImage(neww, newh, myCallback, IntPtr.Zero);
string exFileName = oldfile.Substring(oldfile.LastIndexOf('.')+1).ToLower();
switch (exFileName)
{
case "jpg":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "bmp":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case "jpeg":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "gif":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Gif);
break;
case "png":
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Png);
break;
default:
myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
img.Dispose();
myThumbnail.Dispose();
}
}
private static bool ThumbnailCallback()
{
return false;
}
#endregion
}
}