Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • Leaders
Posted

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.

Posted

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

C#
Posted

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:

Posted

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);

C#
Posted

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.

C#

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...