0

我已经搜索了我的问题的答案并尝试了许多解决方案但无法解决

我有一个 Windows 窗体应用程序,我必须在图片框中动态设置图像,我已经使用以下代码成功地做到了这一点

    pictureBox2.Image = Image.FromFile("C:/Users/user/Documents/College Work/4th Year/Advanced Net/Projects/Game/Resources/" + Quiz1FromFile.Q1Ans + ".jpg"););

如何修改上述路径,以便在我移动项目位置时它可以工作。“游戏”是 windows 窗体项目的名称

4

2 回答 2

1

The best approach is to add an ApplicationSettings in the settings tab of your project property.
Call this property, for example, "ImagePath" and set this property to your default development path.

After doing that, you will have an entry in your app.config file (that becomes, when compiled YourApplicationName.exe.Config) with the value of your default path.

<applicationSettings>
    <YourAppNamespace.Properties.Settings>
        <setting name="ImagePath" serializeAs="String">
            <value>C:/Users/user/Documents/College Work/4th ......</value>
        </setting>
    <(YourAppNamespace.Properties.Settings>
</applicationSettings>

This file will be distributed with your application and you could change that path using your deployment procedure.

In code you could refer to the path stored in the config file using

string imagePath = Properties.Settings.Default.ImagePath;
pictureBox2.Image = Image.FromFile(Path.Combine(imagePath, Quiz1FromFile.Q1Ans,  ".jpg"));

In this way you don't hard code the path to your image folder inside the application and you don't need to handle relative paths. You have only to set this value on the target machine to the storage of your image files.

于 2013-10-25T21:22:25.837 回答
0

你需要做两件事。首先,文件名应该是与可执行文件所在位置相关的相对路径。第二件事是您需要确保图像确实存在。您可以使用构建后事件将图像复制到 DestinationFolder 路径。

看看这里

于 2013-10-25T21:18:45.930 回答