0

ユーザーが選択したフォルダの画像を表示しようとしています。画像のないレコードを選択すると、次のような画像が表示されます。

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;
                }
            }
4

3 に答える 3

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;

}
于 2012-06-08T10:09:37.047 に答える
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;
                }
            }
        }
于 2012-06-08T10:11:03.863 に答える
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;
于 2012-06-08T10:21:10.503 に答える