windows应用程序 代码如下 本来是要写成windows服务的
先用应用程序测试一下。
大概是写一个文件夹监控程序,一旦文件夹中有图片类文件操作,则在另一个文件夹的对应路径下生成此图的缩略图。
问题:在调试的时候,不会报错,功能全部都有。
但是,在执行的时候,只要在监视的图片中拷贝一张图片进去,就会报错,提示内存不足。虚心请教原因。
补充:生成缩略图的方法是网上找的,还换过方法,都是一样的症状。
开发环境 vs2008 sp1
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
}
}
解决方案 »
- 磁盘打不来,提示未被格式化
- 请教一个特殊的href模拟点击的问题。href="javascript:fGoto();"
- c# 操作 Resource.resx 资源文件 谢谢大家关注!
- 关于using
- 首次编译时出现问题,之后就没事了,怪!怪!怪!怪!怪!怪!怪!怪!
- ASP.NET(C#)+Access中关于照片上传以及显示照片连接的问题
- 一段sql语句查询出来的结果怎么放到DataTable里头去?
- 请较高手!!
- 在WPF中如何使用xaml修改Textbox聚集后的背景色?
- 哪位大侠有具体全面的.net做的mis项目体统及源代码参考一下,经验体会也好,感激不尽,再给分
- GridView 前几列固定 后边列可以拖动 怎么实现?
- 关于C#中BinaryReader以及数据存储的问题.请高手指点一下
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
}
}
这句常会报内存不足的错误,特别是图片本身不能被正确识别,或是文件已经损坏,以及5M以上的超高清的图片,
我常用Backgroundworker来处理这些事情
加一句
img=null; 感觉有效一些
我用的是vista的示例图片 应该不会有问题吧 而且我只拷贝了一张
先用文件流处理一下 然后再使用就ok了!
/// <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;
}