Image directory..

Lanc1988

Contributor
Joined
Nov 27, 2003
Messages
508
I have images in my program, so if i just copy the .exe compiled program I made and move it to another computer, it wont open because the image is missing.

I am looking for a code that will know the directory where the file is, because I plan to zip all the needed files and once they are unzipped to any location on someone's computer I want the program to know the directory to it.

So is there a code that can do this? Or is there a code that will let the program open and just not display the images if they aren't found?
 
why not embed the images ( on your vs menu , click project , add an existing item , choose the images , then when they are listed in your project , set them as " Embedded Resource files " )
then you wont need to search for the images.
 
If you are going to do that though, make sure you use a compressed image format because they will be embedded exactly as they are with the same size and bytes.

Then you can get the images like this:

Stream s= System.Reflection.Assembly.GetCallingAssembly().GetManifestResourceStream(name);
if(s==null) return null;
return (Bitmap)Bitmap.FromStream(s);

name is the namespace of your project followed by a dot, then the exact name of the file including the extention.

Mine would be
AW.embeddedImage.png
 
aewarnick.. im not sure where to put:

Stream s= System.Reflection.Assembly.GetCallingAssembly().GetManifestResourceStream(name);
if(s==null) return null;
return (Bitmap)Bitmap.FromStream(s);

Does it go in the code for the PictureBox?

Also im not sure about the namespace of my project? Would that be the name of it, whats its saved as?:confused:
 
I have changed them to 'embedded resource' but now i dont know where to put the code to display them.
 
That was actually part of a whole method sub or function.
All you need is
Stream s= System.Reflection.Assembly.GetCallingAssembly().GetManifestResourceStream(name);
Bitmap b= (Bitmap)Bitmap.FromStream(s);
 
Stream s= System.Reflection.Assembly.GetCallingAssembly().GetManifestResourceStream("YourNamespace.Treasure.jpg");
Bitmap b= (Bitmap)Bitmap.FromStream(s);

Also, Stream is located in the System.IO namespace.
 
Back
Top