I am trying to load a list of image files into an image array. The following code snippet works:

Graphics gImage(hdc);
Image iTiles[intNumTiles] = {L"Image[0].jpg", L"Image[1].jpg", L"Image[3].jpg", ...};

gImage.DrawImage(&iTiles[0], 100, 50);
gImage.DrawImage(&iTiles[1], 140, 90);
gImage.DrawImage(&iTiles[1], 180, 130);
...

The problem is, I don't know how many image files there are, until a run the program, and search a folder for the files. You can see my dilemma. I came across a tool sometime back that would allow me to replace a line of code with a string name. When the program compiles, rather than seeing the string name, it actually reads in the string itself. It would look something like this:

Graphics gImage(hdc);

string sLoadFiles;

for (int i = 0; i < intNumTiles; i++)
{
sLoadFiles = sLoadFiles + ("L\"Image[%d]", i);
}

Image iTiles[intNumFiles] = {sLoadFiles};

for(int j = 0; j < intNumFiles; j++)
{
gImage.DrawImage(&iTiles, 100 + i * 40, 50 + i*40);
}

Obviously the above code is wrong, but does this sound familiar to anybody else?

Recommended Answers

All 5 Replies

>>The problem is, I don't know how many image files there are, until a run the program, and search a folder for the files.

Use either a <vector> or <list> and problem is solved. Both will expand as needed.

>>The problem is, I don't know how many image files there are, until a run the program, and search a folder for the files.

Use either a <vector> or <list> and problem is solved. Both will expand as needed.

fair enough, now I just have to figure out how to make a list/vector of images. I don't think the GDI+ topic on the MSDN page covered anything like this. Even the image array was a shot in the dark. XD

Okay, so this would be the syntax for creating and initializing an image:

Image myImage(L"Image[0]");

This is what I did for a vector:

vector<Image> vTiles;
vTiles.push_back(L"Image[0].jpg");

At least I think that's correct, but when I tried to compile, it gave me this error:
error C2248: 'Gdiplus::Image::Image' : cannot access private member declared in class 'Gdiplus::Image'

This was another problem I ran into. It would appear that there is no constructor for an image object that does not require the object to be initialized at the same time.

Unfortunately, even if I do get this to work, I don't think it will fix my problem. I still have to read the file name into a string variable, and pass that sting value into the vector. Does that make since?

Sorry, I have not worked with images. Someone else will have to help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.