ImageUtilM.cs 4.5 KB
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;
        }
    }
}