图片变更检测

20240723

By wdhuag


目录

前言:

参考:

文件监控:

图片占用问题:

源码:


前言:

由于第三方图像处理软件不能回传图片(正常都能做,这里只是不想做),只能在保存到本地后,读取并显示结果图片。


参考:

C# FileSystemWatcher 实时监控文件的增加、修改、重命名和删除实例-CSDN博客

C#实时监测文件夹变化_c# 实时读取本地文件更新的数据-CSDN博客

C# 获得文件夹下最新的,或更早的文件_c#仅入库最新文件-CSDN博客

C# winform实现文件夹中图片的监控并实时显示更新_csdn winform 实时监测指定文件夹-CSDN博客

C# 加载和传递图片,导致内存溢出的问题_winform 内存溢出-CSDN博客

https://www.cnblogs.com/dongweian/p/15554614.html

https://www.cnblogs.com/TandyChan/p/4819672.html

c# - 将 PictureBox 中的图像调整到尽可能大,同时保持纵横比?_Stack Overflow中文网

windows 文件夹正在使用 “操作无法完成,因为其中的文件夹或文件已在另一程序中打开“ 解决办法_无法装载该文件,因为该文件正在使用-CSDN博客

[总结]C#用于BMP图像显示的方法_c# 拉伸显示bmp-CSDN博客

C#项目中pictureBox控件动态加载显示BMP文件_c# picturebox bmp-CSDN博客

C#中pictureBox控件详细使用方法_c# picturebox-CSDN博客

C#解析RAW图文件并在PictureBox中显示_c#显示raw-CSDN博客

https://www.cnblogs.com/jackrebel/p/3964495.html

c# 判断路径是否存在-CSDN博客

c# 识别图片格式的方法_c#判断图片类型-CSDN博客

C#中Image.FromFile(string path)函数报内存不足错误解决方法_c# 内存不足 fromimage-CSDN博客

关于Image.FromFile方法处理图像出现内存不足的问题_image.fromfile 内存不足-CSDN博客

C# 图片操作(图片读取,保存,转换,传输)_c# png图片通过bitmap显示-CSDN博客


文件监控:

这里选用的是FileSystemWatcher

FileSystemWatcher fileSystemWatcher;
string type;
string filter;

type = comboBox_MonitorType.Text.Replace("*", "");
filter = textBox_Name.Text;
fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.Path = folderPath;
fileSystemWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size;
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.Created += new FileSystemEventHandler(Monitor_Created);
fileSystemWatcher.Changed += new FileSystemEventHandler(Monitor_Changed);
fileSystemWatcher.Deleted += new FileSystemEventHandler(Monitor_Detected);
fileSystemWatcher.Renamed += new RenamedEventHandler(Monitor_Renamed);
fileSystemWatcher.EnableRaisingEvents = true;

图片占用问题:

需要检测文件是否被占用

private bool CheckInUse(string path) {
    int count = 0;
    Stopwatch sw = Stopwatch.StartNew();
Retry:
    if (sw.ElapsedMilliseconds > 3000) {
        sw.Stop();
        return false;
    }
    else {
        count++;
    }
    try {
        FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        fileStream.Close();
        return true;
    }
    catch (Exception ex) {
        goto Retry;
    }
}

读取图片后需要转换避免占用 

private Bitmap FileToBitmap(string path) {
    byte[] bytes = null;
    Stream stream = null;
    MemoryStream memoryStream = null;
    try {
        FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        bytes = new byte[fileStream.Length];
        fileStream.Read(bytes, 0, bytes.Length);
        fileStream.Close();

        stream = new MemoryStream(bytes);
        stream.Read(bytes, 0, bytes.Length);

        stream.Seek(0, SeekOrigin.Begin);
        memoryStream = new MemoryStream(bytes);
        return new Bitmap((Image)new Bitmap(memoryStream));
    }
    catch (ArgumentNullException ex) {
        return null;
    }
    catch (ArgumentException ex) {
        return null;
    }
    finally {
        if (stream != null)
            stream.Close();
    }
}

 图像输出后释放资源避免占用

this.pictureBox_LastImage.Image?.Dispose();
this.pictureBox_LastImage.SizeMode = PictureBoxSizeMode.Zoom;
if (image != null) {
    this.pictureBox_LastImage.Image = (Bitmap)image.Clone();
    image.Dispose();
}
else {
    this.pictureBox_LastImage.Image = null;
}
GC.Collect();

源码:

链接:度盘 
提取码:00yr 


END 

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值