How to know and load all images in a specific fold

2019-04-10 15:30发布

I have an application (C++ Builder 6.0) that needs to know the total of images there are in a specific folder, and then I have to load them: in an ImageList or in a ComboBoxEx... or any other control...

How can I do that?

I know how to load an image in a control, or to save in a TList, or in an ImageList... but How to know how many files files there are in the directory, and how to load every image in it??

I am Sorry about my English.

5条回答
smile是对你的礼貌
2楼-- · 2019-04-10 16:10

I did something like this yesterday with C++ using the boost::filesystem library. However, if you are not using boost already, I would strongly recommend you just use the windows libraries instead. This was my code though in case you're interested:

#include <algorithm>
#include <boost/filesystem.hpp>
#include <set>

namespace fs = boost::filesystem;

typedef std::vector<fs::path> PathVector;

std::auto_ptr<PathVector> ImagesInFolder(const fs::path& folderPath) {
    std::set<std::string> targetExtensions;
    targetExtensions.insert(".JPG");
    targetExtensions.insert(".BMP");
    targetExtensions.insert(".GIF");
    targetExtensions.insert(".PNG");

    std::auto_ptr<PathVector> paths(new PathVector());

    fs::directory_iterator end;
    for(fs::directory_iterator iter(folderPath); iter != end; ++iter) {
        if(!fs::is_regular_file(iter->status())) { continue; }

        std::string extension = iter->path().extension();
        std::transform(extension.begin(), extension.end(), extension.begin(), ::toupper);
        if(targetExtensions.find(extension) == targetExtensions.end()) { continue; }

        paths->push_back(iter->path());
    }

    return paths;
}

This doesn't answer the part of your question about how to actually put the paths into a listbox though.

查看更多
\"骚年 ilove
3楼-- · 2019-04-10 16:13

If you can upgrade to newer version of C++Builder, have a look at TMS AdvSmoothImageListBox, from TMS Software.

TMS AdvSmoothImageListBox

The TMS Smooth Controls are available free for C++Builder 2010 users on the from Embarcadero website.

查看更多
相关推荐>>
4楼-- · 2019-04-10 16:19

Use the Win32 functions FindFirstFile and FindNextFile ...?

查看更多
疯言疯语
5楼-- · 2019-04-10 16:19

There's no practical way to identify every image in an arbitrary folder. Almost anything you can't identify as something else, could be some sort of image. Then again, using steganography, even something you can identify as something else still might be (or contain) at least part of an image as well.

Realistically, you want to pick out a set of formats you want to support, and write code that knows about them. For quite a few purposes, a half dozen formats or so is quite adequate, though the exact half dozen you pick will vary by the type of application -- only a few programs have any use for both bitmapped and vector graphics, for one example.

Once you've decided what you want, DlgDirList is probably the easiest way to list some files. If that isn't flexible enough for your purposes, the next obvious choice is FindFirstFile, FindNextFile, and FindClose.

查看更多
女痞
6楼-- · 2019-04-10 16:20

To get a list of all files in a folder, have a look at the FindFirst and FindNext functions in SysUtils.

Here is an example function which shows how to get a list of files.

void __fastcall TForm1::GetDirList(TStrings *List, const AnsiString SearchStr)
{
 TSearchRec SRec;
 AnsiString TempFName;

 List->Clear();

 // start search
 if (FindFirst(SearchStr, faAnyFile, SRec) == 0)
 {
   do
   { 
     if ((SRec.Attr & faDirectory) != faDirectory) // exclude directories
     {
       List->Add(SRec.Name);
     } // end if
   } 
   while (FindNext(SRec) == 0);

   FindClose(SRec);
 } // end if
} 

Examples: // get list of all files in directory GetDirList(MyStringList, "C:\images*.*");

// get list of all .bmp files in directory
GetDirList(MyStringList, "C:\images\*.bmp");
查看更多
登录 后发表回答