1

I know this is stupid question but please tell me how to publish application with images? I am using

this.BackgroundImage = Image.FromFile("../../images/Blue/Blue_Style1.jpg"); 

it works fine in visual studio but when I publish application images doesn't include and application gives error

T.S.
  • 18,195
  • 11
  • 58
  • 78
Uzair Ali
  • 510
  • 14
  • 32
  • 6
    Hvae you added the file as a resource? – David Pilkington Nov 20 '13 at 11:58
  • 1
    If you add your file in `Resources`, in your code you could use something like `this.BackgroundImage = Properties.Resources.BlueStyle1` – Marco Nov 20 '13 at 11:59
  • 1
    As David and Marco pointed out, the best way is to add this image as a resource to your app, which will then be compiled into the final exe/dll. A simple way to add the resource and use it is through the designer, when setting the BackgroundImage property of a control. Clicking on the `[...]` button next to the `BackgroundImage` property opens the `Select Resource` dialog, where you have the `Import` button which allows you to import the resource and then select it for your background. – vgru Nov 20 '13 at 12:02

4 Answers4

3

Solution 1: Create a dedicated folder for storing the images in your project debug/release folder.

Ex: project/bin/Release/Images/

Access the Images from that folder as below:

String FullPath=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "")+"\\Images\\Blue_Style1.jpg";
this.BackgroundImage =Image.FromFile(FullPath);

Solution 2: accessing Images from Resources file.
Note: first you need to add the Images into Resources.
Here i have added Blue_Style1.jpg file to Resources.

this.BackgroundImage = Properties.Resources.Blue_Style1;

See here for adding images to Resources

Community
  • 1
  • 1
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • Thanks and nice tutorial – Uzair Ali Nov 20 '13 at 13:08
  • Solution 2 is a very good way to automatically deploy images with the application in one single package. However, solution 1 will bring you in conflicts when you use a version control system because you typically exclude the bin-subfolders from version control. If you want to be able to change the images after build and therefore deploy them along with your application in a separate folder, I'd suggest to use Copy to output. – Markus Nov 20 '13 at 13:38
1

For each image you'll need to check the Build Action property to ensure it is copied to the target directory.

Setting the Build Action to Content should work.

Fermin
  • 34,961
  • 21
  • 83
  • 129
1

Modern Answer:

  • Select the jpg in your solutions expolorer
  • In the properties window set Build Action to Content
  • Build your project
  • Open Project Properties
  • Select the Publish tab
  • Click the Application Files... button
  • You should see your jpg (e.g. \Resources\mypicture.jpg)
  • Ensure your jpg Publish Status is set to Include

You can now access your jpg as a file in the executing assembly location.

EllieK
  • 259
  • 4
  • 14
0

This works in Visual Studio because you load the file from two folders above. When running in VS, you usually work in the bin\Debug subfolder, so ..\..\FileName.jpg refers to the file in the project folder.
In order to copy the file to your project output, change the "Copy to output directory" setting to Copy If Newer or Copy Always in the file properties. After that, changet the path of the file as follows:

this.BackgroundImage = Image.FromFile("images/Blue/Blue_Style1.jpg");

When you deploy your application, you have to also copy the images folder.

Markus
  • 20,838
  • 4
  • 31
  • 55