2012-11-14 40 views
3

我的程序有10張圖片,顯示來自我想要的文件夾的圖片。添加下一個按鈕和上一個按鈕允許用戶瀏覽接下來的十張圖片或最近的十張圖片。前十張圖片顯示成功,但下一個按鈕僅在該文件夾正好有二十張圖片時才起作用,如果圖片爲十五張,則會崩潰。 這裏是我的代碼:一個顯示十個圖片箱上的很多圖片

PictureBox[] myPicBoxArray = new PictureBox[10]; 
string path = @"\\Documents\Pictures\Camera"; 

private void Form1_Load(object sender, EventArgs e) 
{ 
    myPicBoxArray[0] = pictureBox1; 
    myPicBoxArray[1] = pictureBox2; 
    myPicBoxArray[2] = pictureBox3; 
    myPicBoxArray[3] = pictureBox4; 
    myPicBoxArray[4] = pictureBox5; 
    myPicBoxArray[5] = pictureBox6; 
    myPicBoxArray[6] = pictureBox7; 
    myPicBoxArray[7] = pictureBox8; 
    myPicBoxArray[8] = pictureBox9; 
    myPicBoxArray[9] = pictureBox10; 
} 

//Show button to display first ten pictures 
private void showButton_Click(object sender, EventArgs e) 
{ 
    string[] files = Directory.GetFiles(path); 
    int i = 0; 
    foreach (string ofile in files) 
    { 
     myPicBoxArray[i].SizeMode = PictureBoxSizeMode.StretchImage; 
     myPicBoxArray[i].Image = Image.FromFile(files[i]); 

     i++; 
     if (i >= 10) 
      break; 
    } 
} 

private void nextButton_Click(object sender, EventArgs e)//The problem is here, 
{ 
    DirectoryInfo fileDir = new DirectoryInfo(path); 
    while (i2 < 10) 
    { 
     myPicBoxArray[i2].SizeMode = PictureBoxSizeMode.StretchImage; 
     if (i2 + 10 < picArrFileNames.Length) 
     { 
      myPicBoxArray[i2].Image = Image.FromFile(picArrFileNames[i2 + 10]); 
     } 
    } 
} 

沒有線索爲前一個按鈕。

+0

後的錯誤。 – Blorgbeard

+2

似乎是對我的家庭作業。給定的代碼不會編譯,並且缺少一些關鍵細節('i2'設置或修改的位置?)。 –

+0

爲什麼你不使用'ImageList'? –

回答

1

您可以簡單地將這些圖像存儲在ImageList中,並使用屬性myImageList.Images.Count來計算ImageList中的圖像數量,而不是使用有限數組。
問題解決.. :)或者去與Lists和使用myList.Count

你可以做這樣的事情:

DirectoryInfo dir = new DirectoryInfo(filePath); 
    foreach (FileInfo file in dir.GetFiles()) 
    {    
     this.myImageList.Images.Add(Image.FromFile(file.FullName)); 
    } 
+0

string [] arrFileNames = Directory.GetFiles(path); ImageList imgList = new ImageList(); 圖片img = null; imgList.Images.Add(img); 這是正確的嗎? –

+0

myImageList.Images.Add(Image.FromFile(filePath)); –

+0

然後我可以寫什麼for循環?這是我的主要問題 –

0

我不知道你爲什麼douing此:

if (i2 + 10 < picArrFileNames.Length) 
    { 
     myPicBoxArray[i2].Image = Image.FromFile(picArrFileNames[i2 + 10]); 
    } 

然而,嘗試一下本作的計數問題,也做了文件擴展名檢查以避免下面MrGreen提供的系統文件。

private void nextButton_Click(object sender, EventArgs e)//The problem is here, 
    { 
     DirectoryInfo fileDir = new DirectoryInfo(path); 
     int count = fileDir.GetFiles().Length;    
     while (i2 < count) 
     { 
      myPicBoxArray[i2].SizeMode = PictureBoxSizeMode.StretchImage; 
      if (i2 + 10 < picArrFileNames.Length) 
      { 
       myPicBoxArray[i2].Image = Image.FromFile(picArrFileNames[i2 + 10]); 
      }            
     }    
    } 

在這裏的某個地方使用此在一個循環的延伸在這裏檢查使用這一個循環延伸檢查

string e = Path.GetExtension("YourFilePathHere"); 
if (e == ".jpeg") 
{ 
    //Do your stuff 
} 
+0

如果該文件夾甚至包含其他文件而不是像'Desktop.ini'或其他系統文件的圖像,該怎麼辦? –

+0

我假設這個文件夾只用於@DarkLi所提供的圖像,「string path = @」\\ Documents \ Pictures \ Camera「;」。 或者我們可以去檢查文件擴展名。 – sajanyamaha

+0

沒有老兄,因爲我上面提到的系統文件會崩潰。嘗試替代...或者在你的答案中提及(否則如果用戶認爲這是正確的方法可能會有問題)希望你理解:) –