Click to See Complete Forum and Search --> : Image File Arrays


Roadkill247
February 2nd, 2007, 12:23 PM
Not quite sure where to put this one, but I am trying to create function which will display an image in a picturebox depending on a random integer which is created.

char images[4][25] = {
"..\\images\\plain.jpg",
"..\\images\\forest.jpg",
"..\\images\\water.jpg",
"..\\images\\wall.jpg"
};

int random_integer = rand()%4)+1;
pictureBox1->Load(images[random_integer]);
pictureBox2->Load(images[random_integer]);
....

I tried this code, but seem to have problems with the Load, in that it won't take it as it isnt a file name, and thus can't create the map.

Thanks in advance.

Alin
February 2nd, 2007, 12:42 PM
what type is pictureBox1?

Roadkill247
February 2nd, 2007, 12:56 PM
what type is pictureBox1?


Just the standard Visual Studio Component, for on a form.

Paul McKenzie
February 2nd, 2007, 12:58 PM
int random_integer = rand()%4)+1;
pictureBox1->Load(images[random_integer]);
pictureBox2->Load(images[random_integer]);

I tried this code, but seem to have problems with the Load, in that it won't take it as it isnt a file name, and thus can't create the map.
What would happen if you do a Load(images[4])? You get an access violation.

Your random number will produce random numbers in the range of 1 - 4. That is not what you want. You want to produce random numbers in the range of 0 -3, since in C++ arrays start at 0.

Also, if the problem is Load, why introduce the random numbers into the problem? Just call Load with a hard coded image name and get that to work first. Then you try the random number thing once you can get a Load to work properly.

Regards,

Paul McKenzie

Roadkill247
February 3rd, 2007, 02:49 PM
What would happen if you do a Load(images[4])? You get an access violation.

In the actual code, on the end of the random number generator, i add +1 to make it between 1-4. Ye, it doesn't like the fact that it is not the location of an actual image.


Also, if the problem is Load, why introduce the random numbers into the problem? Just call Load with a hard coded image name and get that to work first. Then you try the random number thing once you can get a Load to work properly.

I had the Load function working with the actual image locations, but I am trying to create it so that, when a user clicks a button, then the images will change to another one, or the same one, with this being random, depending on the integer value it was given.

Roadkill247
February 10th, 2007, 05:18 PM
Anyone any ideas ?

jcjudt
February 10th, 2007, 05:57 PM
In the actual code, on the end of the random number generator, i add +1 to make it between 1-4. Ye, it doesn't like the fact that it is not the location of an actual image.


Not sure you understood paul correctly. By adding a one to the random number, you can have a number that is out of bounds in your array. you declared an array with four elements...indexes are 0-3, if you try to access the element at the index 4, you'll get an access violation.

jcjudt
February 10th, 2007, 06:01 PM
of course, when your program crashes and breaks into the debugger, if you weren't already debugging, just take a look at what the value of random_integer is. Then look and see what the value of the string you are passing to Load is. Let us know.

Also, it would be helpful if you could post the error message here.

Roadkill247
February 11th, 2007, 09:04 AM
The error I get is this:

C:\documents and settings\greg\my documents\uni\year 2\pathfinding assignment\pathfinding assignment\Form1.h(1792) : error C2664: 'void System::Windows::Forms::PictureBox::Load(System::String ^)' : cannot convert parameter 1 from 'char [25]' to 'System::String ^'
Reason: cannot convert from 'char *' to 'System::String ^'
No user-defined-conversion operator available, or
Cannot convert an unmanaged type to a managed type


Also have taken out the +1 in the random function.

Roadkill247
February 11th, 2007, 09:06 AM
tried also:

pictureBox1->Load("images[random_integer]");

with the quotations in, it runs then, but breaks when it is called.

Paul McKenzie
February 11th, 2007, 09:53 AM
The error I get is this:

C:\documents and settings\greg\my documents\uni\year 2\pathfinding assignment\pathfinding assignment\Form1.h(1792) : error C2664: 'void System::Windows::Forms::PictureBox::Load(System::String ^)' : cannot convert parameter 1 from 'char [25]' to 'System::String ^'
Reason: cannot convert from 'char *' to 'System::String ^'
No user-defined-conversion operator available, or
Cannot convert an unmanaged type to a managed typeThat error and code doesn't make sense in this forum, since none of that is "traditional" C++.

If you are writing using managed C++, you're in the wrong forum. There is a Managed C++ forum for this type of code.

Regards,

Paul McKenzie

Paul McKenzie
February 11th, 2007, 10:02 AM
tried also:

pictureBox1->Load("images[random_integer]");

with the quotations in, it runs then, but breaks when it is called.1) From your previous post, you are using managed C++, which is off-topic for this forum. What is the prototype of the Load() function? What is the argument type that is supposed to be passed to it? If it is one of those managed types, then like I stated, you're in the wrong forum.

2) Ask yourself, what does that line that you posted do? What is happening when the argument is "images[random_integer]"? What is going on when you put quotes around it? It becomes a string, so the Load function is looking for a file named "images[random_integer]". Is this what you want?

To be honest with you, I don't know what the difficulty is. What is the value of images[random_integer]? You have a debugger, what is the value? Is it the name of one of the files?

Regards,

Paul McKenzie

ovidiucucu
February 11th, 2007, 10:19 AM
[ Redirected thread ]

jcjudt
February 11th, 2007, 10:26 AM
I agree with Paul, this is a managed C++ issue and thus you might be in the wrong forum...still, I'd like to be helpfull...

I know nothing about managed C++ but I'm guessing that the PictureBox::Load function probably wants a String object but you are passing it an array of chars. Surely there is some way to convert between the two (I'm surprised it won't do the conversion for you automatically). This also explains why you were able to get this to work when you used literal values as those probably were converted automatically to be a managed type.

If it were me, I'd probably try to do something like this to see if it works:


pictureBox1->Load(new String(images[random_integer]));


That may work, but it would probably be inefficient on a larger scale as String objects are probably immutable and there should be no reason to have to use "new" with "String".

Anyways, like i said, i don't know much about managed C++, but try to understand the concept I'm passing to you. Even though an array of chars makes a "string", it is an array of pirmitives and in managed C++, probably regarded as unsafe...try creating a String from the .NET framework out of your char array and pass that to the Load function.

Hope this helps.

TheCPUWizard
February 11th, 2007, 10:26 AM
Don't use a char array. Use a sting array!