2017-07-03 73 views
0

我在C#中使用Movie Maker時間軸控制嘗試隨機化Windows Movie Maker中圖片的順序。我設置了表單,但我似乎遇到了導入照片的問題。我的代碼需要能夠導入多個隨機命名的圖像(例如:IMG_xxxx.JPG)。我可以單獨導入一個專門命名的圖像,但是當我嘗試使用foreach循環時,它會失敗。ForEach循環,爲每個隨機命名圖像執行命令

我的代碼:

private void btnOpen_Click(object sender, EventArgs e) 
     { 

      openFolder.Description = "Open a folder containing pictures for the slideshow!"; 

      if (openFolder.ShowDialog() == DialogResult.OK) 
      { 
       // this code right here 
       string folderPath = openFolder.SelectedPath; 

       string[] fileArray = Directory.GetFiles(folderPath, "*.JPG"); 
       foreach (string file in fileArray) 
       { 
        folderPath = folderPath + "\\" + file; 
        float duration = 4; 
        float startpos = 0; 
        timelineControl.AddImageClip(timelineControl.GetImageTrackIndex(), folderPath, startpos, (startpos + duration)); 
        startpos = startpos + 4; 
       } 

       /* string image = "C:\\Users\\OSR\\Desktop\\jpgs\\img.JPG"; 
       float duration = 4; 
       timelineControl.AddImageClip(timelineControl.GetImageTrackIndex(), image, 0, duration); 
       */ 
      } 
      else 
      { 
       MessageBox.Show("Next time, select a folder and click open!", "Selection Cancelled", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 

     } 

全碼:https://pastebin.com/MrB928Ri

計劃:http://imgur.com/a/bikxU

+0

時,它的失敗會發生什麼「? –

+0

你爲什麼要改變循環中的'folderPath',爲下一個文件摧毀它?你應該使用一個新的'filepath'變量。 – NetMage

+1

你正在'疊加'你的var「folerPath」 – Cadburry

回答

1

您將會破壞迴路中的folderPath

var folderPath = openFolder.SelectedPath; 

var fileArray = Directory.GetFiles(folderPath, "*.JPG"); 
float duration = 4; 
float startpos = 0; 
foreach (var file in fileArray) { 
    var filePath = [email protected]"{folderPath}\{file}"; 
    timelineControl.AddImageClip(timelineControl.GetImageTrackIndex(), filePath, startpos, (startpos + duration)); 
    startpos += 4; 
}