Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I've only recently started using Visual C++ in the .NET environment but have quite a bit of previous C++ experiemce. I'm embarassed to admit that i'm having a problem loading an image into a picture box at runtime.

 

In the button click event handler i have the following:

 

openFileDialog1->ShowDialog();

pictureBox1->Image = openFileDialog1->FileName;

 

When I compile i get a type error (string to image). I can see why i'm getting the error but can't see how to get round it. (i usually use delphi for windows apps!)

 

Any help appreciated.

Posted

Not that I've used c++ in .net but if it's .net my advice would be to go thru the System.Drawing; And from there you could use the Image-class which should have a FromFile()-method which creates an image object that could be passed on to the picture-box.

 

PS. No idea if this would work in managed C++ or whatever it's called. I use mainly C# for everything .Net.

Posted

Thanks for that. I've altered the code and now it compiles but the image on the picturebox remains blank.

 

Image *face;

openFileDialog1->ShowDialog();

face->FromFile(openFileDialog1->FileName );

pictureBox1->Image = face;

pictureBox1->Update(); //also tried using refresh

 

Any ideas?!

Posted

It looks like your not storing the fetched image correctly. FromFile is static and you should store it in "face" instead then give face to the pic box.

 

Image *face;

openFileDialog1->ShowDialog();

face = Image->FromFile( openFileDialog1->FileName );

pictureBox1->Image = face;

 

 

since I don't know that language I'm just guessing here.

this is the C#.net equivelent which i'm positive would work.

 

 

OpenFileDialog openFileDialog1 = new OpenFileDialog();

Image face;

openFileDialog1.ShowDialog();

face = Image.FromFile( openFileDialog1.FileName );

pictureBox1.Image = face;

Posted
It looks like your not storing the fetched image correctly. FromFile is static and you should store it in "face" instead then give face to the pic box.

 

Image *face;

openFileDialog1->ShowDialog();

face = Image->FromFile( openFileDialog1->FileName );

pictureBox1->Image = face;

 

I had the same thought and tried the same thing myself but now the code won't compile! There seems to be an error on the line:

 

face = Image->FromFile( openFileDialog1->FileName );

 

The debugger tells me i've made a class type error. I tried substituting . for-> but to no avail. This is really starting to bug me now!

Posted

Try this.

 

OpenFileDialog* openFileDialog1 = new OpenFileDialog();

Bitmap* MyImage;

if(openFileDialog1->ShowDialog() == DialogResult::OK)

{

MyImage = new Bitmap(openFileDialog1->FileName);

pictureBox1->Image = dynamic_cast<Image*>(MyImage);

}

Posted

No problem, lol, yes it does seems as a strange way of doing it. I don't even really know C++.

 

Perhaps you should consider emigrating to C#!? it's a lot cleaner,straight forward and logical, atleast from where i'm standing, I mean, if your gonna use .net anyway might as well go all the way :D

 

PS.. didn't mean to sound too missionary.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...