Hi,

I have a form application that has a number of pictureboxes. As the program runs it changes the pictures in the boxes depending on buttons pressed on the form via a function 'updateGUI'.

The program runs for a limited period and eventually i get an error:


An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
Additional information: Parameter is not valid.


when the updateGUI function is run. The error occurs on the line (underlined):

if ( MyImage5 != nullptr ) 
{ 
delete MyImage5; 
} 
picturebox_5->SizeMode = PictureBoxSizeMode::StretchImage; 
[U]MyImage5 = gcnew Bitmap( <filetodisplay system string> ); [/U]
picturebox_5->Image = dynamic_cast<Image^>(MyImage5);

I have checked the program, which is far too large to post, and the only thing i can think of that is causing the error is recursivity.

Essentially, the program loads up the form but doesn't close it down until the user exits the program. While the form is on screen, functions in the main.cpp file are called many times over, however, the program is designed so that no function runs more than once without user input (via buttons) in between each call.

Would the problem be solved with some sort of periodic memory dump regarding the pictureboxes?

Any help would be greatly appreciated.

Re: C++ 'System.ArgumentException' in System.Drawing.dll 80 80

What is generating

<filetodisplay system string>

?

the only thing i can think of that is causing the error is recursivity

Is there a proper base case for it? (I would think this would generate some kind of stack problem instead of the invalid parameter, though)

Re: C++ 'System.ArgumentException' in System.Drawing.dll 80 80

<filetodisplay system string> just represents the location of the file i am inserting into the picturebox and isn't itself code, hence the <>

Re: C++ 'System.ArgumentException' in System.Drawing.dll 80 80

I understand. I was asking what code is generating the file name that is used? That method might be giving "bad" strings if you've overstepped your set of filenames.

Re: C++ 'System.ArgumentException' in System.Drawing.dll 80 80

oops wrong msg...

Re: C++ 'System.ArgumentException' in System.Drawing.dll 80 80

Oh, thanks for looking at this,

An example function is below. The value i am passing into the function is an integer, via a line of code in the form:

show_picturebox_3(25);

for example. There are files with the names 1.png, 2.png...25.png...100.png in my c:\ drive so i don't think thats an issue (pls see below):

public: 
void show_picturebox_3(int filetodisplay) 
{ 
std::ostringstream osstream; 
osstream << filetodisplay; // converting integer to string
std::string filetodisplay_string = osstream.str(); // converting integer to string
osstream.str(""); // clear for later use
filetodisplay_string = "C:\\" + filetodisplay_string + ".png"; // use new string to make address of file in string form
String ^filetodisplay_system_string = gcnew String(filetodisplay_string .c_str()); // convert filetodisplay_string to system string to use in gcnew line below 
if ( MyImage3 != nullptr ) 
{ 
delete MyImage3; 
} 
picturebox_3->SizeMode = PictureBoxSizeMode::StretchImage; 
MyImage3 = gcnew Bitmap(filetodisplay_system_string); 
picturebox_3->Image = dynamic_cast<Image^>(MyImage3); 
}

So i think i've got the string types correct. The thing is that the program works sporadically so i thought it might be some sort of memory overflow, but that isn't the error message i'm getting.

i saw one person on google saying reduce the size of the image, but the ones i'm using are only tiny, like 4k in size ...

Re: C++ 'System.ArgumentException' in System.Drawing.dll 80 80

I would try outputting filetodisplay_system_string values via System::Diagnostics::Debug::WriteLine() (they'll end up in the output window)and make sure there's nothing funky there. Unless it's a flukey error there is something wrong with one of the strings that you are passing to that constructor.

Re: C++ 'System.ArgumentException' in System.Drawing.dll 80 80

Thanks Jonsca - I did this and checked the names and i had an incorrectly named image file - was 'copy of'...

Thanks for you're help

Be a part of the DaniWeb community

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