开发者

How to know and load all images in a specific folder?

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 th开发者_C百科e directory, and how to load every image in it??

I am Sorry about my English.


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.


Use the Win32 functions FindFirstFile and FindNextFile ...?


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.


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");


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

How to know and load all images in a specific folder?

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜