title: Visual C++, System Argument Exception in System Drawing (Windows Form)
body: <p>This error shows up when run the program and try to load an image:</p>
  
  <p>A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll
  An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll</p>
  
  <p>Additional information: The parameter is invalid.</p>
  
  <p>Here is my code:</p>
  
  <p>Basically, there is a numericUpDown, a button, an openFileDialog and a pictureBox. The user changes the numericUpDown's value, depending on which picture he wants to load (the user doesn't have to open the openFileDialog). For example, if the user chooses "3" as a value for the numericUpDown, the FileName of the openFileDialog will be:</p>
  
  <pre><code>Public:
      void Set_FilePath()
      {
          int n = (int)numericUpDown1-&gt;Value;
  
          switch (n)
          {
          case 1: openFileDialog1-&gt;FileName = "C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"; break;
          case 2: openFileDialog1-&gt;FileName = "C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"; break;
          case 3: openFileDialog1-&gt;FileName = "C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg"; break;
          case 4: openFileDialog1-&gt;FileName = "C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg"; break;
          case 5: openFileDialog1-&gt;FileName = "C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"; break;
          case 6: openFileDialog1-&gt;FileName = "C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg"; break;
          case 7: openFileDialog1-&gt;FileName = "C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"; break;
          case 8: openFileDialog1-&gt;FileName = "C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg"; break;
          }
  
      }
  private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                   Bitmap^ myImage;
  
                   Set_FilePath();
                   myImage = gcnew Bitmap( openFileDialog1-&gt;FileName );
                   pictureBox1-&gt;SizeMode =                  PictureBoxSizeMode::StretchImage;
                   pictureBox1-&gt;Image = dynamic_cast &lt;Image^&gt; (myImage);
  
           }
  </code></pre>
  
  <p>My attempt to fix it:</p>
  
  <p>I thought that i didn't copy the directions of the images correctly. So i changed the code to:</p>
  
  <pre><code>if(openFileDialog1-&gt;ShowDialog() == System::Windows::Forms::DialogResult::OK) 
               {
                   MessageBox::Show(openFileDialog1-&gt;FileName);
                   myImage = gcnew Bitmap( openFileDialog1-&gt;FileName );
                   pictureBox1-&gt;SizeMode = PictureBoxSizeMode::StretchImage;
                   pictureBox1-&gt;Image = dynamic_cast &lt;Image^&gt; (myImage);
               }
  </code></pre>
  
  <p>This is working perfectly. Also, a messagebox that shows the filename of openFileDialog is appeared... The directions of the images that are correct... I don't know what is wrong with my program. The problem is that i don't want the openFiledialog to be appeared.</p>
  
  <p>(I am using Visual Studio C++ 2010, the application is made in windows form), Any help would be appreciated.
  Thanks..</p>