public void imageCompareThreadStart() { if (startIndex >= imageList.Count()) { endFlag = true; return; } if (endIndex >= imageList.Count()) { endIndex = imageList.Count(); } Merge2 merge = new Merge2(); for (int i = startIndex; i < endIndex; i++) { count++; imageKeyList.Add(merge.MergePic(imageList[i])); if (i % 20 == 0 && System.Diagnostics.Process.GetProcessesByName(threadName).ToList().Count == 0) { return; } } endFlag = true; }
public void imageThreadStart() { if (startIndex >= imageList.Count()) { endFlag = true; return; } if (endIndex >= imageList.Count()) { endIndex = imageList.Count(); } Merge2 merge = new Merge2(); for (int i = startIndex; i < endIndex; i++) { count++; imageKeyList.Add(merge.MergePic(imageList[i])); } endFlag = true; }
public void mergeThreadStart() { if (checkBox_merge_Flag == null) { checkBox_merge_Flag = false; } if (checkBox_sameDel_Flag == null) { checkBox_sameDel_Flag = false; } if ((Boolean)checkBox_merge_Flag) { //messageFlg = 1; //全部文件合并到第一个文件夹 String mainFloder = pathList[0].ToString(); String renameFile; for (int a = 1; a < pathList.Count; a++) { try { string[] fileList = Directory.GetFiles(pathList[a].ToString(), "*.jpg", SearchOption.TopDirectoryOnly); for (int i = 0; i < fileList.Length; i++) { int index = 1; renameFile = Path.GetFileName(fileList[i]); while (File.Exists(mainFloder + @"\" + renameFile)) //重复时重命名 { renameFile = Path.GetFileNameWithoutExtension(fileList[i]) + "_" + index + Path.GetExtension(fileList[i]); index++; } try { File.Move(fileList[i], mainFloder + @"\" + renameFile); } catch (Exception) { if (File.GetAttributes(fileList[i]).ToString().IndexOf("ReadOnly") != -1) { System.IO.File.SetAttributes(fileList[i], System.IO.FileAttributes.Normal); //去除只读 File.Move(fileList[i], mainFloder + @"\" + renameFile); } } if (i % 10 == 0) { statusStr = pathList[a].ToString() + " 图片合并中---" + i; if (System.Diagnostics.Process.GetProcessesByName(threadName).ToList().Count == 0) { return; } } if (stop_Flag) { return; } } statusStr = pathList[a].ToString() + " 图片合并中---完成"; } catch (Exception) { } } } //画像KEY取得 Merge2 merge = new Merge2(); ArrayList imageKeyList = new ArrayList(); int imageCount = 0; int aCount = pathList.Count; if ((Boolean)checkBox_merge_Flag) { aCount = 1; } for (int a = 0; a < aCount; a++) { string[] baseFileList = Directory.GetFiles(pathList[a].ToString(), "*.jpg", SearchOption.TopDirectoryOnly); Thread[] th = new Thread[threadNum]; ThreadClass2[] threadClass = new ThreadClass2[threadNum]; Boolean threadOKFlg = true; //处理文件线程分割用 int imageListCut = (int)Math.Ceiling(baseFileList.Length * 1.0 / threadNum); int startCut = 0; int endCut = 0; for (int i = 0; i < threadNum; i++) { endCut = startCut + imageListCut; threadClass[i] = new ThreadClass2(); threadClass[i].startIndex = startCut; threadClass[i].endIndex = endCut; threadClass[i].imageList = baseFileList; threadClass[i].threadName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; th[i] = new Thread(new ThreadStart(threadClass[i].imageCompareThreadStart)); th[i].Start(); startCut = endCut; } while (true) { Thread.Sleep(1000); if (System.Diagnostics.Process.GetProcessesByName(threadName).ToList().Count == 0) { return; } imageCount = 0; for (int i = 0; i < threadNum; i++) { imageCount = imageCount + threadClass[i].count; } statusStr = "图片分析中---" + imageCount; //启动进程完了否 threadOKFlg = true; for (int i = 0; i < threadNum; i++) { threadOKFlg = threadOKFlg && threadClass[i].endFlag; } //启动进程完了时处理 if (threadOKFlg) { for (int i = 0; i < threadNum; i++) { imageKeyList.AddRange(threadClass[i].imageKeyList); } statusStr = "图片分析---完成"; break; } //关闭 if (stop_Flag) { return; } } } string key = ""; myDictionary = new Dictionary <string, string>(); //比较 for (int i = 0; i < imageKeyList.Count - 1; i++) { for (int j = i + 1; j < imageKeyList.Count; j++) { // textFlg = ((ImageID_KIND2)imageKeyList[j]).Path + "1"; // backgroundWorker1.ReportProgress(i); // 完全相同直接删除 if ((Boolean)checkBox_sameDel_Flag && merge.GetSameLevel((ImageID_KIND2)imageKeyList[i], (ImageID_KIND2)imageKeyList[j]) == 1.0) { try { // textFlg = ((ImageID_KIND2)imageKeyList[j]).Path + "2"; // backgroundWorker1.ReportProgress(i); File.Delete(((ImageID_KIND2)imageKeyList[j]).Path); imageKeyList.Remove(imageKeyList[j]); j--; } catch (Exception) { if (File.GetAttributes(((ImageID_KIND2)imageKeyList[j]).Path).ToString().IndexOf("ReadOnly") != -1) { System.IO.File.SetAttributes(((ImageID_KIND2)imageKeyList[j]).Path, System.IO.FileAttributes.Normal); //去除只读 File.Delete(((ImageID_KIND2)imageKeyList[j]).Path); j--; } } continue; } else if (merge.GetSameLevel((ImageID_KIND2)imageKeyList[i], (ImageID_KIND2)imageKeyList[j]) >= samePercent) { // textFlg = ((ImageID_KIND2)imageKeyList[j]).Path + "3"; // backgroundWorker1.ReportProgress(i); key = ((ImageID_KIND2)imageKeyList[i]).Path; if (myDictionary.ContainsKey(key)) { myDictionary[key] = myDictionary[key] + ";" + ((ImageID_KIND2)imageKeyList[j]).Path; } else { myDictionary.Add(key, ((ImageID_KIND2)imageKeyList[j]).Path); } // textFlg = ((ImageID_KIND2)imageKeyList[j]).Path + "4"; // backgroundWorker1.ReportProgress(i); } //关闭 if (stop_Flag) { return; } } if (i % 10 == 0) { // backgroundWorker1.ReportProgress(i); statusStr = "图片比较中---" + i; if (System.Diagnostics.Process.GetProcessesByName(threadName).ToList().Count == 0) { return; } } } statusStr = "图片比较---完成"; //处理结束,显示相似图片一览 showSamePicListTOForm delegateTemp = delegate() { //this.label3.Text = "比较完了······"; //按钮活性 /*this.button1.Enabled = true; * this.button2.Enabled = true; * this.button3.Text = "开始"; * this.button4.Enabled = true; * this.button5.Enabled = true; * this.button6.Enabled = true; * this.button7.Enabled = true; * this.button8.Enabled = true;*/ //显示一览输出 //this.listView_samePic.BeginUpdate(); //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度 foreach (KeyValuePair <string, string> kvp in myDictionary) { sameImgList.Add(new SameImg(kvp.Key, kvp.Value)); } // listView_samePic.ItemsSource = sameImgList; //初期选择第一行 /* try * { * ((ListViewItem)listView_samePic.Items[0]).IsSelected = true; * //this.listView_samePic.Select(); * } * catch (Exception) * { * }*/ }; this.Dispatcher.Invoke(delegateTemp); }
//长时间处理放到后台作,不让页面假死掉 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { int imageCount = 0; //进度用 BackgroundWorkBean bean = (BackgroundWorkBean)e.Argument; try { if (bean.checkBox1Flag) { messageFlg = 1; //全部文件合并到第一个文件夹 String mainFloder = bean.pathArr[0]; String renameFile; for (int a = 1; a < bean.pathArr.Length; a++) { try { string[] fileList = Directory.GetFiles(bean.pathArr[a], "*.jpg", SearchOption.TopDirectoryOnly); for (int i = 0; i < fileList.Length; i++) { imageCount++; int index = 1; renameFile = Path.GetFileName(fileList[i]); while (File.Exists(mainFloder + @"\" + renameFile)) //重复时重命名 { renameFile = Path.GetFileNameWithoutExtension(fileList[i]) + "_" + index + Path.GetExtension(fileList[i]); } try { File.Move(fileList[i], mainFloder + @"\" + renameFile); } catch (Exception) { if (File.GetAttributes(fileList[i]).ToString().IndexOf("ReadOnly") != -1) { System.IO.File.SetAttributes(fileList[i], System.IO.FileAttributes.Normal); //去除只读 File.Move(fileList[i], mainFloder + @"\" + renameFile); } } if (i % 10 == 0) { backgroundWorker1.ReportProgress(imageCount); } if (backgroundWorker1.CancellationPending) { e.Cancel = true; return; } } } catch (Exception) { } } } //画像KEY取得 Merge2 merge = new Merge2(); ArrayList imageKeyList = new ArrayList(); messageFlg = 2; imageCount = 0; int aCount = bean.pathArr.Length; if (bean.checkBox1Flag) { aCount = 1; } for (int a = 0; a < aCount; a++) { string[] baseFileList = Directory.GetFiles(bean.pathArr[a], "*.jpg", SearchOption.TopDirectoryOnly); /* for (int i = 0; i < baseFileList.Length; i++) * { * imageCount++; * imageKeyList.Add(merge.MergePic(baseFileList[i])); * * if (i % 10 == 0) * { * backgroundWorker1.ReportProgress(imageCount); * } * if (e.Cancel || backgroundWorker1.CancellationPending) * { * return; * } * }*/ Thread[] th = new Thread[threadNum]; ThreadClass[] threadClass = new ThreadClass[threadNum]; Boolean threadOKFlg = true; //处理文件线程分割用 int imageListCut = (int)Math.Ceiling(baseFileList.Length * 1.0 / threadNum); int startCut = 0; int endCut = 0; for (int i = 0; i < threadNum; i++) { endCut = startCut + imageListCut; threadClass[i] = new ThreadClass(); threadClass[i].startIndex = startCut; threadClass[i].endIndex = endCut; threadClass[i].imageList = baseFileList; th[i] = new Thread(new ThreadStart(threadClass[i].imageThreadStart)); th[i].Start(); startCut = endCut; } while (true) { Thread.Sleep(1000); imageCount = 0; for (int i = 0; i < threadNum; i++) { imageCount = imageCount + threadClass[i].count; } backgroundWorker1.ReportProgress(imageCount); //启动进程完了否 threadOKFlg = true; for (int i = 0; i < threadNum; i++) { threadOKFlg = threadOKFlg && threadClass[i].endFlag; } //启动进程完了时处理 if (threadOKFlg) { for (int i = 0; i < threadNum; i++) { imageKeyList.AddRange(threadClass[i].imageKeyList); } break; } //关闭 if (backgroundWorker1.CancellationPending) { for (int i = 0; i < threadNum; i++) { th[i].Abort(); } e.Cancel = true; return; } } } string key = ""; myDictionary = new Dictionary <string, string>(); //比较 messageFlg = 3; for (int i = 0; i < imageKeyList.Count - 1; i++) { for (int j = i + 1; j < imageKeyList.Count; j++) { // textFlg = ((ImageID_KIND2)imageKeyList[j]).Path + "1"; // backgroundWorker1.ReportProgress(i); // 完全相同直接删除 if (bean.checkBox1Flag && merge.GetSameLevel((ImageID_KIND2)imageKeyList[i], (ImageID_KIND2)imageKeyList[j]) == 1.0) { try { // textFlg = ((ImageID_KIND2)imageKeyList[j]).Path + "2"; // backgroundWorker1.ReportProgress(i); File.Delete(((ImageID_KIND2)imageKeyList[j]).Path); imageKeyList.Remove(imageKeyList[j]); j--; } catch (Exception) { if (File.GetAttributes(((ImageID_KIND2)imageKeyList[j]).Path).ToString().IndexOf("ReadOnly") != -1) { System.IO.File.SetAttributes(((ImageID_KIND2)imageKeyList[j]).Path, System.IO.FileAttributes.Normal); //去除只读 File.Delete(((ImageID_KIND2)imageKeyList[j]).Path); j--; } } continue; } else if (merge.GetSameLevel((ImageID_KIND2)imageKeyList[i], (ImageID_KIND2)imageKeyList[j]) >= samePercent) { // textFlg = ((ImageID_KIND2)imageKeyList[j]).Path + "3"; // backgroundWorker1.ReportProgress(i); key = ((ImageID_KIND2)imageKeyList[i]).Path; if (myDictionary.ContainsKey(key)) { myDictionary[key] = myDictionary[key] + ";" + ((ImageID_KIND2)imageKeyList[j]).Path; } else { myDictionary.Add(key, ((ImageID_KIND2)imageKeyList[j]).Path); } // textFlg = ((ImageID_KIND2)imageKeyList[j]).Path + "4"; // backgroundWorker1.ReportProgress(i); } if (backgroundWorker1.CancellationPending) { e.Cancel = true; return; } } // if (i % 10 == 0) { backgroundWorker1.ReportProgress(i); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }