public MonitoringService() { InitializeComponent(); if (ImD == null) { ImD = new ImgsDetectionHelper(); } int timelast = 0; if (int.TryParse(timelagStr, out timelast)) { if (timelast > 1) { time = new Timer(timelast * 1000); } else { time = new Timer(timelag); ErrorCollectHelper.ErrorLog("配置文件错误!timelag配置不符合条件,采取默认30s内置配置"); } } else { time = new Timer(timelag); ErrorCollectHelper.ErrorLog("配置文件错误!timelag配置不符合条件,采取默认30s内置配置"); } empTable = new DataTable(); empTable.Columns.Add("path", typeof(string)); thisTable = empTable; }
private void Time_Elapsed(object sender, ElapsedEventArgs e) { lock ("asdad") { thisTable = empTable; //empTable.Clear(); ErrorCollectHelper.ErrorLog("当前操作数据量:" + thisTable.Rows.Count.ToString()); //foreach (DataRow item in thisTable.Rows) //{ // try // { // ImD.Monitor(item["path"].ToString()); // empTable.Rows.Remove(item); // } // catch (Exception ex) // { // empTable.Rows.Add(item); // ErrorCollectHelper.ErrorLog(ex.ToString()); // } //} for (int i = 0; i < thisTable.Rows.Count; i++) { try { if (ImD.Monitor(thisTable.Rows[i]["path"].ToString())) { empTable.Rows.Remove(thisTable.Rows[i]); } } catch (Exception ex) { ErrorCollectHelper.ErrorLog(ex.ToString()); } } } //pathList.AddRange(addList); //addList.Clear(); //foreach (var itemPath in pathList) //{ // try // { // ImD.Monitor(itemPath); // } // catch (Exception ex) // { // errorList.Add(itemPath); // ErrorCollectHelper.ErrorLog(ex.ToString()); // } //} //pathList.Clear(); //pathList.AddRange(errorList); //errorList.Clear(); }
public bool Monitor(string path) { try { DecideImageNorms(path); } catch (Exception ex) { ErrorCollectHelper.ErrorLog(ex.ToString()); return(false); } return(true); }
protected override void OnStart(string[] args) { try { //这里是监测操作 imgWatcher.WatcherStart(); //这里是处理操作 StartSomething(); } catch (Exception ex) { ErrorCollectHelper.ErrorLog(ex.ToString()); } }
/// <summary> /// 图片检测,大小和格式检测 /// </summary> private void DecideImageNorms(string imgPath) { bool isAccordWithSize = true; QuestionModel QuestionPath = new QuestionModel(); if (!File.Exists(imgPath)) { return; } using (FileStream fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read)) { if (fs.Length < minSize || fs.Length > maxSize) { QuestionPath.error = "[文件大小 不符规范]"; QuestionPath.path = imgPath; QuestionPath.fileName = FieldInterception(imgPath); ErrorCollectHelper.ImgErrorLog(QuestionPath); isAccordWithSize = false; } fs.Close(); fs.Dispose(); } if (!isAccordWithSize) { CopyFile(QuestionPath); File.Delete(QuestionPath.path); isAccordWithSize = true; return; } if (postfix == null || postfix.ToUpper() != "*.JPG") { return; } Image img = Image.FromFile(imgPath); if (img.RawFormat.Equals(ImageFormat.Jpeg)) { return; } else if (img.RawFormat.Equals(ImageFormat.Png)) { QuestionPath.error = "[图片本质为PNG格式]"; QuestionPath.path = imgPath; } else if (img.RawFormat.Equals(ImageFormat.Bmp)) { QuestionPath.error = "[图片本质为BMP格式]"; QuestionPath.path = imgPath; } else { QuestionPath.error = "[图片的本质格式未知]"; QuestionPath.path = imgPath; } QuestionPath.fileName = FieldInterception(imgPath); img.Dispose(); CopyFile(QuestionPath); File.Delete(QuestionPath.path); ErrorCollectHelper.ImgErrorLog(QuestionPath); }