发布 Windows 窗体应用程序 - 图像去哪里?

发布于 2024-12-16 23:28:53 字数 308 浏览 0 评论 0原文

有关 Windows 窗体的真实 R101 问题。

我创建了第一个 Windows 应用程序来打印发票。我有一张图片是公司徽标(png)。该应用程序在 VS2010 中调试时运行良好 - 我将图像放在 bin 文件夹中。

现在我已经发布了它并将其安装在另一台机器上,我收到此异常错误:

System.IO.FileNotFoundException:ice-logo-bw.png

所以问题是我应该将图像放置在哪里,以便在发布时将其包含在内?我尝试将其放在根文件夹中,但没有成功。

A real R101 question on Windows Forms.

I have created my first Windows application to print invoices. I have one image which is the company logo (png). The app works great in VS2010 when debugging - I placed the image in the the bin folder.

Now I have published it and installed it on another machine I get this exception error:

System.IO.FileNotFoundException: ice-logo-bw.png

So the question is where do I place the image so it gets included when I publish? I have tried putting it in the root folder with no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

じее 2024-12-23 23:28:53

您应该将该图像包含在项目中 - 可能与其他图像一起位于其自己的文件夹下。

然后,您应该在此图像上设置构建操作 - 右键单击​​,选择属性并选择“如果较新则复制”或“始终复制”。

另一种方法是将其添加为资源,但这意味着您还需要从资源而不是文件系统中获取它。

You should include the image in the project - possibly under a folder of its own with other images.

You should then set the Build Action on this image - right click, select properties and select "Copy if Newer" or "Copy always".

An alternative is to add it as a resource, but this means you need to also fetch it from the resources rather than the filesystem.

居里长安 2024-12-23 23:28:53

正如其他人已经说过的那样,您需要确保当您将应用程序部署/安装到另一台计算机时,您的图像文件也会被复制 - 最合适的位置可能是项目的输出目录(bin/ 目录(已编译的 .exe 所在的位置) - 您可以将文件的“复制到输出目录”属性设置为“如果较新则复制”,以便 Visual Studio 自动为您执行此操作。

另一件需要考虑的事情是如何加载图像 - 如果您使用相对路径,那么您可能会发现您的程序无法找到您的图像,即使它们与可执行文件一起存在,因为工作目录不同,请尝试从在您的开发计算机上的命令提示符下查看这种情况的发生:

:: Make sure that the current working directory is different from the one app.exe is in
C:\> path\to\my\program\app.exe

如果您的代码看起来有点像这样:

var image = LoadImage("myfile.png");

那么这可能会失败,因为您的程序正在 c:\myfile.png 查找图像,而不是C:\path\to\my\program\app.exe。您应该修改您的代码,使其看起来有点像这样:


另一种替代方法是使用嵌入资源。这稍微复杂一些,但优点是您的图像嵌入到输出 .exe 本身中 - 不需要额外的文件,因此减少了因所需文件已被删除/移动/未被删除而无法找到的可能性安装时复制的。

string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "myfile.png");
var image = LoadImage(path);

As others have already said you need to make sure that when you deploy / install your application to another machine that your image files are also copied - the most suitable place is probably the output directory of your project (the bin/ directory where the compiled .exe goes) - you can set the "Copy to Output Directory" property for the files to be "Copy if newer" to have Visual Studio do this automatically for you.

Another thing to consider is how you load your images - if you are using relative paths then you might find that your program fails to find your image even if they are present alongside your executable because the working directory is different, try running your program from the command prompt on your development machine to see this happening:

:: Make sure that the current working directory is different from the one app.exe is in
C:\> path\to\my\program\app.exe

If your code looks a little like this:

var image = LoadImage("myfile.png");

Then this will probably fail because your program is looking for the image at c:\myfile.png instead of C:\path\to\my\program\app.exe. You should modify your code to look a little like this:


Another alternative would be to use embedded resources. This is slightly more complex however has the advantage that your images are embedded into the output .exe itself - no additional files are needed so it reduces the chances that required files can't be found because they have been deleted / moved / weren't copied during installation.

string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "myfile.png");
var image = LoadImage(path);
三生路 2024-12-23 23:28:53

这不是放置情况(放置文件的位置),而是 Build Action & 复制到 OutputDirectory 属性 - 可以在属性窗口 (Visual Studio) 中访问。
将文件(图像)添加到项目后设置以下属性:

Build Action = None (depend on your application)
Copy to Output Directory  = Copy always

文档参考此处

It's not place case (where you put the files) but Build Action & Copy to OutputDirectory properties - can be accesed in properties window (Visual Studio).
After added files (images) to project set following properties:

Build Action = None (depend on your application)
Copy to Output Directory  = Copy always

Documentation reference here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文