ImageUtilM.cs
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSA_V.Common;
namespace TSA_V.DeviceLibrary
{
public class ImageUtilM
{
private static string defPath = @"D:\image\";
public static bool canSavePic(string path = "", int checkZhao = 50)
{
try
{
if (path == "")
{
path = defPath;
}
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string volume = path.Substring(0, path.IndexOf(':'));
long freespace = GetHardDiskSpace(volume);
int zhao = (int)(freespace / 1024);
if (zhao < checkZhao)
{
LogUtil.error(volume + "盘剩余空间:" + zhao + "G, 不再保存图片");
return false;
}
}
catch (Exception ex)
{
LogUtil.error($"canSavePic {path} error :" + ex.ToString());
return false;
}
return true;
}
private static long GetHardDiskSpace(string str_HardDiskName)
{
long totalSize = 0;
str_HardDiskName = str_HardDiskName + ":\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == str_HardDiskName)
{
totalSize = drive.TotalFreeSpace / (1024 * 1024);
}
}
return totalSize;
}
public static bool AutoDelFiles(string path, int maxDay = 5, int minDay = 2)
{
if (Directory.Exists(path).Equals(false))
{
Directory.CreateDirectory(path);
}
for (int day = maxDay; day >= minDay; day--)
{
if (!canSavePic(path))
{
DeleteOldFiles(path, day);
}
else
{
return true;
}
}
return canSavePic();
}
private static void DeleteOldFiles(string path = "", int days = 7)
{
if (path == "")
{
path = defPath;
}
try
{
LogUtil.info($"DeleteOldFiles 开始删除文件夹[{path}][{days}]天前的文件");
if (!Directory.Exists(path) || days < 1) return;
var now = DateTime.Now;
foreach (var f in Directory.GetFileSystemEntries(path).Where(f => File.Exists(f)))
{
DateTime createTime = File.GetCreationTime(f);
var elapsedTicks = now.Ticks - createTime.Ticks;
var elapsedSpan = new TimeSpan(elapsedTicks);
if (elapsedSpan.TotalDays > days)
{
File.Delete(f);
LogUtil.info($"DeleteOldFiles 删除文件: {f} ,创建时间:{createTime.ToLongTimeString()}");
}
}
}
catch (Exception ex)
{
LogUtil.error($"DeleteOldFiles {path} {days} error :" + ex.ToString());
}
}
public static string SaveImageToFile(string deviceName, string cameraName, Image bitmap)
{
string date = DateTime.Now.ToString("yy-MM-dd-HH-mm-ss-") + DateTime.Now.Millisecond;
string dire = @"D:\image\" + deviceName.Trim().Replace('_', '-') + @"\" + cameraName.Trim().Replace('_', '-').Replace(':', '-') + @"\";
string iamgeName = date + ".bmp";
try
{
if (AutoDelFiles(dire))
{
bitmap.Save(dire + iamgeName, ImageFormat.Bmp);
//bitmap.Dispose();
LogUtil.info(deviceName + " 【" + cameraName + "】 保存图片到【" + dire + iamgeName + "】成功");
}
}
catch (Exception ex)
{
LogUtil.error("保存" + deviceName + " 【" + cameraName + "】的图片到【" + dire + iamgeName + "】出错" + ex.ToString());
}
return dire + iamgeName;
}
}
}