Search code examples
c++winformsvisual-c++visual-studio-2012openfiledialog

importing a picture from an openfiledialog into a picturebox, in visual c++


M stuck in importing a picture from an openfiledialog into a picturebox, in visual c++ windows form at Visual studio 2012... i searched it on different forums, and found one possible solution:-

private void btnLoad_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog op = new OpenFileDialog();
        op.Title = "Select a picture";
        op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
            "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
            "Portable Network Graphic (*.png)|*.png";
        if (op.ShowDialog() == true)
        {
            imgPhoto.Source = new BitmapImage(new Uri(op.FileName));
        }

    }

But in this solution or other solutions close to this one, does not allow me to create " new OpenFileDialog(); "

another solution, offered by microsoft for cursor file was...

private:
System::Void button1_Click(System::Object *  sender,
  System::EventArgs *  e)
{
  // Displays an OpenFileDialog so the user can select a Cursor.
  OpenFileDialog * openFileDialog1 = new OpenFileDialog();
  openFileDialog1->Filter = "Cursor Files|*.cur";
  openFileDialog1->Title = "Select a Cursor File";

  // Show the Dialog.
  // If the user clicked OK in the dialog and
  // a .CUR file was selected, open it.
  if (openFileDialog1->ShowDialog() == DialogResult::OK)
  {
     // Assign the cursor in the Stream to
     // the Form's Cursor property.
     this->Cursor = new
        System::Windows::Forms::Cursor(
        openFileDialog1->OpenFile());
  }
}

same problem in this too....

can any one suggest the easiest approach to do the required task


Solution

  • In your first example, you are trying to use C# sintax. In C++/CLI you won't use . but the ->, neither new or * for handles. You actually need gcnew and ^.
    Apart from this, look at this simple code:

    Note: To be sure this example will compile, create a new project called TestImage, add a button called btnLoad and a picturebox called pbImage.

    In c++ it is better to separate your header file (.h ) from your c++ file ( .cpp ). In your header file, declare just the prototype of the click event:

    private: System::Void btnLoad_Click(System::Object^  sender, System::EventArgs^  e);
    

    In your .cpp file, you should have something like this:

    #include "stdafx.h"
    #include "Form1.h"
    
    using namespace TestImage;
    
    System::Void Form1::btnLoad_Click(System::Object^  sender, System::EventArgs^  e) {
    
            OpenFileDialog^ ofd = gcnew OpenFileDialog();
            //insert here the filter if you want
            //ofd->Filter..
            if (ofd->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
    
                 pbImage->Image = Image::FromFile(ofd->FileName);
            }
        }
    

    Remember to resize accordingly to your image pbImage and you will be fine.

    Hope this one helps.