jedbartlet Posted September 10, 2004 Posted September 10, 2004 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. Quote
Varghjärta Posted September 11, 2004 Posted September 11, 2004 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. Quote
jedbartlet Posted September 11, 2004 Author Posted September 11, 2004 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?! Quote
Varghjärta Posted September 11, 2004 Posted September 11, 2004 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; Quote
jedbartlet Posted September 11, 2004 Author Posted September 11, 2004 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! Quote
Varghjärta Posted September 11, 2004 Posted September 11, 2004 Try this. OpenFileDialog* openFileDialog1 = new OpenFileDialog(); Bitmap* MyImage; if(openFileDialog1->ShowDialog() == DialogResult::OK) { MyImage = new Bitmap(openFileDialog1->FileName); pictureBox1->Image = dynamic_cast<Image*>(MyImage); } Quote
jedbartlet Posted September 11, 2004 Author Posted September 11, 2004 Problem Solved! It works thanks a lot. It seems a strange way of doing it but i'm happy! Thanks. :D Quote
Varghjärta Posted September 12, 2004 Posted September 12, 2004 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.