其實搞這個小程序只是為了更好的解決現在遇到的問題,什么問題呢?現在在做關于圖像處理的課題,用CCD批量拍攝了幾百副圖像,命名方式為1.bmp,2.bmp,3.bmp。。。 ,按順序來的,因為有些圖像因為模糊或者不符合處理條件,我手動將其剔除,于是有了剛才提到的問題,現在的大量圖片文件名不連續了,用Matlab處理時不方便。于是寫了個C#小程序用來自動排列圖像。代碼如下
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Windows.Forms;
6 // 自動重命名
7 namespace renamebmp
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 int first = 1; //起始圖片編號
14 int last = 301; //結束圖片編號
15 int temp;
16 string path1; //尋找到的第一個空位置
17 string path2; //尋找到的空位置后的最小編號圖片文件路徑
18 int count = 0; //圖片數量
19 string temppath = "";
20 for (int k = first; k <= last; k++)
21 {
22 temppath = "F://test6//" + k.ToString() + ".bmp";
23 if (File.Exists(temppath))
24 count++;
25 }
26 for (int i = first; i <= count; i++)
27 {
28 temp = i + 1;
29 path1 = "F://test6//" + i.ToString() + ".bmp";
30 if (!File.Exists(path1))
31 {
32 path2 = "F://test6//" + temp.ToString() + ".bmp";
33 while (!File.Exists(path2))
34 {
35 temp++;
36 if (temp == last)
37 {
38 path2 = "F://test6//" + last.ToString() + ".bmp";
39 break;
40 }
41 path2 = "F://test6//" + temp.ToString() + ".bmp";
42 }
43 File.Move(path2, path1); // 使用move方法重命名
44 }
45 }
46 MessageBox.Show("排序完畢,排序后圖片數量為 "+count.ToString());
47 }
48 }
49 }
新聞熱點
疑難解答