困惑,在使用FileSystemWatcher监视图片后,总是显示内存不足

elemeNtchen 2008-09-11 08:13:30
用了FileSystemWatcher监视一个目录有没有图片进来,然后显示到picturebox上,如果copy一个小的图片来没问题,但拷贝一个大一点的图片(500k),代码:Image.FromFile(strFileName);就提示内存不足

如果单独用 String strFileName = System.Windows.Forms.Application.StartupPath + "\\1.jpg";
pictureBox_Show.Image = Image.FromFile(strFileName);
图片即使大于2M都没问题,困惑阿

以下是我提示内存不足的代码

public FileSystemWatcher MyWatch = new FileSystemWatcher();

private void PhotoForm_Load(object sender, EventArgs e)
{
//配置文件系统监视器

MyWatch.Path = Application.StartupPath;
MyWatch.Filter = "*.JPG";
MyWatch.IncludeSubdirectories = false;
//添加文件系统监视器事件句柄
MyWatch.Created += new FileSystemEventHandler(OnCreatedOrDeleted);
MyWatch.Deleted += new FileSystemEventHandler(OnCreatedOrDeleted);
MyWatch.EnableRaisingEvents = true;
}

private void OnCreatedOrDeleted(object sender, FileSystemEventArgs e)
{
//当在当前目录中创建和删除文件时刷新图片

if (e.ChangeType == WatcherChangeTypes.Created)
{
String strFileName = e.FullPath;

ShowPicture(strFileName);
}

}

private void ShowPicture(string strFileName)
{
pictureBox_Show.Image = Image.FromFile(strFileName);
}
...全文
139 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
elemeNtchen 2008-09-11
  • 打赏
  • 举报
回复
奇怪的问题,单独显示没问题的,就是放到FileSystemWatcher 响应事件中就不行了
LQknife 2008-09-11
  • 打赏
  • 举报
回复
没用过 帮你顶
yagebu1983 2008-09-11
  • 打赏
  • 举报
回复
没遇到过!!
关注!!
蓝色木 2008-09-11
  • 打赏
  • 举报
回复
学习
palmax 2008-09-11
  • 打赏
  • 举报
回复
你每次 ShowPicture 时,上一次的 Image.FromFile(strFileName) 的 GDI资源 没有释放啊!!
sxmonsy 2008-09-11
  • 打赏
  • 举报
回复
楼主的意思是引发了连索反应?
windboyzsj 2008-09-11
  • 打赏
  • 举报
回复 1
OnCreated 事件在创建文件后立即发生。如果正在将文件复制或传送至一个受监视的目录,OnCreated 事件将立即发生,随后发生一个或多个 OnChanged 事件。

下面是我的代码
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;

namespace FileWatcher
{
public partial class Form1 : Form
{
public FileSystemWatcher MyWatch = new FileSystemWatcher();
String strFileName="";
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

MyWatch.Path = Directory.GetCurrentDirectory();
MyWatch.Filter = "*.JPG";
MyWatch.IncludeSubdirectories = false;
//添加文件系统监视器事件句柄
MyWatch.Created += new FileSystemEventHandler(OnCreatedOrDeleted);
MyWatch.Deleted += new FileSystemEventHandler(OnCreatedOrDeleted);
MyWatch.Changed += new FileSystemEventHandler(MyWatch_Changed);
MyWatch.EnableRaisingEvents = true;
MyWatch.SynchronizingObject = this; //最好加上这一句
}

void MyWatch_Changed(object sender, FileSystemEventArgs e)
{
ShowPicture(strFileName);
}

private void OnCreatedOrDeleted(object sender, FileSystemEventArgs e)
{
//当在当前目录中创建和删除文件时刷新图片

if (e.ChangeType == WatcherChangeTypes.Created)
{
strFileName = e.FullPath;
}
}

private void ShowPicture(string strFileName)
{
try
{pictureBox1.Image = Image.FromFile(strFileName,false);
}
catch (System.Exception e)
{
MessageBox.Show(e.Message);
}

}
}
}
hanjun1024 2008-09-11
  • 打赏
  • 举报
回复
估计是线程同步的问题。
private void ShowPicture(string strFileName)
{
if(InvokeRequired)
//切换到主线程
//Invoke(ShowPicture);
else
pictureBox_Show.Image = Image.FromFile(strFileName);
}

具体的应该是建立一个委托,然后用那个委托进行线程切换的调用。我手头没有开发环境,盲打对我还是有点难度的,呵呵。

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧