2012-06-08 68 views
0

我試圖顯示從文件夾中的圖像,用戶selected.and如果他slected不具有任何圖片的記錄,它會顯示其被稱爲圖像C#檢查是否有圖片應用

Empty.png

這裏是我寫的代碼。我能怎樣改變,它會適合我在寫的交代?(這個問題的頂部)

  string[] fileEntries = Directory.GetFiles(@"C:\Projects_2012\Project_Noam\Files\ProteinPic"); 

      foreach (string fileName in fileEntries) 
      { 
       if (fileName.Contains(comboBox1.SelectedItem.ToString())) 
       { 
        Image image = Image.FromFile(fileName); 
        // Set the PictureBox image property to this image. 
        // ... Then, adjust its height and width properties. 
        pictureBox1.Image = image; 
        pictureBox1.Height = image.Height; 
        pictureBox1.Width = image.Width; 
       } 
      } 
+0

這是什麼意思「記錄沒有任何圖片」?你想知道fileName是圖像嗎? –

+0

用戶在comboBox.it中選擇一條記錄,不是針對他選擇圖片的具體記錄,顯示「空」圖片 – Noam650

回答

0

我不認爲你需要通過在每個文件迭代目錄達到你想要的目標

 Image image; 

     string imagePath = System.IO.Path.Combine(@"C:\Projects_2012\Project_Noam\Files\ProteinPic", comboBox1.SelectedItem.ToString()); 
     if (System.IO.File.Exists(imagePath)) 
     { 
      image = Image.FromFile(imagePath); 
     } 
     else 
     { 
      image = Image.FromFile(@"C:\Projects_2012\Project_Noam\Files\ProteinPic\Empty.png"); 
     } 

     pictureBox1.Image = image; 
     pictureBox1.Height = image.Height; 
     pictureBox1.Width = image.Width; 
2
foreach (string fileName in fileEntries) 
{ 
    if (fileName.Contains(comboBox1.SelectedItem.ToString())) 
    { 
    pictureBox1.Image = Image.FromFile(fileName);    
    } 
    else 
    { 
    pictureBox1.Image = ImageFromFile("Empty.png");     
    } 

    // Set the PictureBox image property to this image. 
    // ... Then, adjust its height and width properties. 
    pictureBox1.Image = image; 
    pictureBox1.Height = image.Height; 
    pictureBox1.Width = image.Width; 

} 
1
string[] fileEntries = Directory.GetFiles(@"C:\Projects_2012\Project_Noam\Files\ProteinPic"); 

     if (fileEntries.Length == 0) 
     { 
      Image image = Image.FromFile("Path of empty.png"); 
      pictureBox1.Image = image; 
      pictureBox1.Height = image.Height; 
      pictureBox1.Width = image.Width; 
     } 
     else 
     { 
      foreach (string fileName in fileEntries) 
      { 
       if (fileName.Contains(comboBox1.SelectedItem.ToString())) 
       { 
        Image image = Image.FromFile(fileName); 
        pictureBox1.Image = image; 
        pictureBox1.Height = image.Height; 
        pictureBox1.Width = image.Width; 
       } 
      } 
     }