在進行影像處理工作時, 經常需要批次處理,

處理的對象可能是某個目錄夾下的一序列影像 {0.bmp, 1.bmp, 2.bmp,….},

可以存放在一個List<string>的容器內, 紀錄該目錄夾特定附檔名的檔案名稱

   1: public static List<string> searchFileNames(string sDirPath, string fileExtension)
   2:         {
   3:             List<string> list = new List<string>();
   4:             if (!Directory.Exists(sDirPath)) return null;
   5:             foreach (string filePath in System.IO.Directory.GetFiles(sDirPath, fileExtension))
   6:             {
   7:                 list.Add(System.IO.Path.GetFileName(filePath));
   8:             }
   9:             return list;
  10:         }

"True love never goes smooth", 搜尋出來結果有點失望

一種做法是將位數補足2位數,但我不想這麼做…因為遲早會遇到

image

利用檔案名稱(不含副檔名)進行排序, 可以達到令人滿意的檔案名稱排序

image

   1: if (BrowserTBx.Text.Length > 0)
   2:            {
   3:                _lastDir = BrowserTBx.Text;
   4:                _extensions = extensionTBx.Text;
   5:                List<string> list = FileTool.searchFileNames(_lastDir, _extensions);
   6:                filesLB.Items.Clear();
   7:                if(checkBox1.Checked)
   8:                try
   9:                {
  10:                    list.Sort(delegate(string x, string y)
  11:                    {
  12:                        int x1 = Convert.ToInt16(Path.GetFileNameWithoutExtension(x));
  13:                        int y1 = Convert.ToInt16(Path.GetFileNameWithoutExtension(y));
  14:                        return x1.CompareTo(y1);
  15:                    });
  16:                }
  17:                catch (Exception exception) { MessageBox.Show(exception.Message); }
  18:                if (list.Count > 0)
  19:                {
  20:                    foreach (string item in list)
  21:                    {
  22:                        filesLB.Items.Add(item);
  23:                    }
  24:                }
  25:            }

當使用者點選某個檔案名稱時, 希望可以自動載入圖片

   1: private void LoadImage()
   2:        {
   3:            string filePath = _lastDir +"\\" + filesLB.Text;
   4:            try
   5:            {
   6:                if (extensionTBx.Text == "*.jpg" |
   7:                    extensionTBx.Text == "*.bmp" |
   8:                    extensionTBx.Text == "*.png" |
   9:                    extensionTBx.Text == "*.tif")
  10:                    imageBox1.Load( filePath );
  11:            }
  12:            catch
  13:            {
  14:            }
  15:        }

=======================================================================

(1)第一種狀況是檔名即編號+.bmp

上面比較函式採用匿名函式, 也可以自訂義類別如CompareByNum

   1: public class CompareByNum : IComparer<string>
   2: {
   3:     public int Compare(string x, string y) // implement IComparer interface
   4:      {
   5:          int x1 = Convert.ToInt16(Path.GetFileNameWithoutExtension(x));
   6:          int y1 = Convert.ToInt16(Path.GetFileNameWithoutExtension(y));
   7:          return x1.CompareTo(y1);
   8:      } 
   9: }

(2) 第二種狀況是 編號_X座標_Y座標.bmp

image

自訂義類別如CompareByNumUnderline

   1: public class CompareByNumUnderline : IComparer<string>
   2:     {
   3:         public int Compare(string x, string y) // implement IComparer interface
   4:         {
   5:             string[] x_split = x.Split('_');
   6:             string[] y_split = y.Split('_');
   7:             
   8:             int x1 = Convert.ToInt16(x_split[0]);
   9:             int y1 = Convert.ToInt16(y_split[0]);
  10:             return x1.CompareTo(y1);
  11:         }
  12:     }

如果是預設排序則是長得像這樣

image

(3) 第三種狀況是 列數_行數.bmp

image

   1: public class CompareByRowColUnderline : IComparer<string>
   2:     {
   3:         public int Compare(string x, string y) // implement IComparer interface
   4:         {
   5:             string[] x_split = Path.GetFileNameWithoutExtension(x).Split('_');
   6:             string[] y_split = Path.GetFileNameWithoutExtension(y).Split('_');
   7:             int stride = 12;
   8:             int row1 = Convert.ToInt16(x_split[0]);
   9:             int row2 = Convert.ToInt16(y_split[0]);
  10:             int col1 = Convert.ToInt16(x_split[1]);
  11:             int col2 = Convert.ToInt16(y_split[1]);
  12:             int ind1 = row1 * stride + col1;
  13:             int ind2 = row2 * stride + col2;
  14:             return ind1.CompareTo(ind2);
  15:         }
  16:     }

================================================================

呼叫方式

   1: private void SearchSubroutine()
   2:         {
   3:             if (BrowserTBx.Text.Length > 0)
   4:             {
   5:                 _lastDir = BrowserTBx.Text;
   6:                 _extensions = extensionTBx.Text;
   7:                 List<string> list = FileTool.searchFileNames(_lastDir, _extensions);
   8:                 filesLB.Items.Clear();
   9:                 
  10:                 try
  11:                 {
  12:                     switch (comboBox1.SelectedIndex)
  13:                     {
  14:                         case 0:
  15:                             break;
  16:                         case 1:
  17:                             list.Sort(new CompareByNum());
  18:                             break;
  19:                         case 2:
  20:                             list.Sort(new CompareByNumUnderline());
  21:                             break;
  22:                         case 3:
  23:                             list.Sort(new CompareByRowColUnderline());
  24:                             break;
  25:                     }
  26:                 }
  27:                 catch (Exception exception) { MessageBox.Show(exception.Message); }
  28:                 if (list.Count > 0)
  29:                 {
  30:                     foreach (string item in list)
  31:                     {
  32:                         filesLB.Items.Add(item);
  33:                     }
  34:                 }
  35:             }
  36:         }



參考資料

  1. 1. C#文件和文件文件夹按时间、名称排序-顺序与倒序
  2. 2. C# 取得副檔名的方法
  3. 3. C# 系统应用之TreeView控件 (一).显示树状磁盘文件目录及加载图标
  4. 4. Problem sorting lists using delegates
  5. 5. 如何:分割字串 (C# 程式設計手冊)
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()