Green_qaue
Green_qaue

Reputation: 3661

Add Images to windows form so they work after publish

I am currently setting my images in my windows form like this:

pictureBox1.Image = Image.FromFile("C:\\Users\\User\\Documents\\Visual Studio 2013\\Projects\\WindowsFormsApplication1\\WindowsFormsApplication1\\Krum\\11.jpg");

But this will not work after I publish the product and load it from another computer at another location.

How do I add images to my project so that they will work after I publish and send it to another computer? What path do I use and where do I need to add them?

UPDATE:

Trying to find the path to my file after adding it to properties is not working very well. In my prperties the file looks like this:

internal static System.Drawing.Bitmap one {
        get {
            object obj = ResourceManager.GetObject("one", resourceCulture);
            return ((System.Drawing.Bitmap)(obj));
        }
    }

And then I try to use it like this:

System.Reflection.Assembly thisExe;
        thisExe = System.Reflection.Assembly.GetExecutingAssembly();
        System.IO.Stream file =
            thisExe.GetManifestResourceStream("WindowsFormsApplication1.Properties.Resources.one");
        this.pictureBox1.Image = Image.FromStream(file);

Upvotes: 2

Views: 1270

Answers (2)

Raheel Khan
Raheel Khan

Reputation: 14777

If you do not want to embed the images in the assembly file, you could always add them to your solution, set the property "Copy to Output Directory" to "Always", then use the following code to acces relative paths:

var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var filename = Path.Combine(directory, "Images", "Image1.png");

Additionally, if you are using a setup project, also include the folder/files to install along with the assembly.

Upvotes: 0

Seany84
Seany84

Reputation: 5596

Add the image file to the project, and set the Build Action property to Embedded Resource in Solution Explorer and then use something like:

System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = 
    thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
this.pictureBox1.Image = Image.FromStream(file);

reference: http://msdn.microsoft.com/en-us/library/aa287676(v=vs.71).aspx

Upvotes: 2

Related Questions