How to publish application with image

2019-06-14 10:31发布

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

3条回答
一纸荒年 Trace。
2楼-- · 2019-06-14 10:49

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

查看更多
看我几分像从前
3楼-- · 2019-06-14 10:57

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.

查看更多
走好不送
4楼-- · 2019-06-14 11:09

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.

查看更多
登录 后发表回答