【问题标题】:Retrieve image from multiple folders从多个文件夹中检索图像
【发布时间】:2017-10-07 10:19:48
【问题描述】:

如何在多个文件夹中查找图像?这是我当前的代码,但它只从一个文件夹中检索。

string imgFilePath = @"C:\Users\may\Documents\Visual Studio 2013\WebSites\EV\photo\" 
                      + textBoxEmplNo.Text + ".jpg";
if (File.Exists(imgFilePath))
{
    pictureBox1.Visible = true;
    pictureBox1.Image = Image.FromFile(imgFilePath);
}
else
{
    //Display message that No such image found
    pictureBox1.Visible = true;
    pictureBox1.Image = Image.FromFile(@"C:\Users\may\Documents\Visual Studio 2013\WebSites\EV\photo\No-image-found.jpg");
}

【问题讨论】:

  • 如何检索?从 1 个文本框?随机?只选一条路?你必须解释你的问题
  • @EpicKip 是的,从 1 个文本框中搜索图像。从那个文本框中,它检查图像来自哪个文件,然后在图片框中显示图像。
  • 你将如何从 1 个文本框中搜索多个文件?我必须在那里输入什么来获取什么文件?你必须至少解释你希望它如何工作
  • @EpicKip 我有 1000 多个来自三个文件的员工图像。每个文件包含 300 张图像。当用户在文本框员工编号输入值时,员工的图像应显示在图片框中。当我有三个文件时,我不知道如何在图片框中显示图像。如何从每个文件中读取以在图片框中显示图像?

标签: c# winforms visual-studio


【解决方案1】:

我稍微更改了您的代码以在多个文件夹中搜索图像:

//BaseFolder that contains the multiple folders 
string baseFolder = "C:\\Users\\myName\\Desktop\\folderContainingFolders";
string[] employeeFolders = Directory.GetDirectories( baseFolder );

//Hardcoded employeeNr for test and png because I had one laying around
string imgName =  "1.png";

//Bool to see if file is found after checking all
bool fileFound = false;

foreach ( var folderName in employeeFolders )
{
    var path = Path.Combine(folderName, imgName);
    if (File.Exists(path))
    {
        pictureBox1.Visible = true;
        pictureBox1.Image = Image.FromFile(path);
        fileFound = true;
        //If you want to stop looking, break; here 
    }
}
if(!fileFound)
{
    //Display message that No such image found
    pictureBox1.Visible = true;
    pictureBox1.Image = Image.FromFile(@"C:\Users\may\Documents\Visual Studio 2013\WebSites\EV\photo\No-image-found.jpg");
}

【讨论】:

  • @Miza 没问题很高兴我能提供帮助
  • 你介意我提问吗?如何从具有多个文件夹的一个文件夹中检索?这意味着 string[] employeeFolders = { "folderNameA", "folderNameB", "folderNameC" }; 不需要。因为我不想在添加文件夹时再次调整代码。谢谢
  • @Miza 你的意思是遍历某个“基本”文件夹中的所有文件夹?
  • 我可以发布代码,可以通过基本文件夹为您填充数组吗?
  • 您可以在之前的代码中进行编辑吗?因为我试过了,还是不行。
【解决方案2】:

试试下面给出的代码 sn-p。您只需将新条目添加到文件夹路径字符串数组中即可添加 n 个要查看的文件夹路径。

String[] possibleFolderPaths = new String[] { "folderpath1" , "folderpath2", "folderpath3" };
        String imgExtension = ".jpg";
        Boolean fileFound = false;
        string imgFilePath = String.Empty;

        // loop through each folder path to check if file exists in it or 
not.
        foreach (String folderpath in possibleFolderPaths)
        {
            imgFilePath = String.Concat(folderpath, 
textBoxEmplNo.Text.Trim(),imgExtension);

            fileFound = File.Exists(imgFilePath);

            // break if file found.
            if (fileFound)
            {
                break;
            }

        }

        if (fileFound)
        {
            pictureBox1.Visible = true;
            pictureBox1.Image = Image.FromFile(imgFilePath);

            imgFilePath = string.Empty;
        }
        else
        {
            //Display message that No such image found
            pictureBox1.Visible = true;
            pictureBox1.Image = Image.FromFile(@"C:\Users\may\Documents\Visual Studio 2013\WebSites\EV\photo\No-image-found.jpg");
        }

【讨论】:

    猜你喜欢
    • 2021-11-03
    • 1970-01-01
    • 2017-05-06
    • 2019-12-08
    • 2017-07-18
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多