C# FileNotFound 当我使用 Image.FromFile()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15729873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FileNotFound when I use Image.FromFile()
提问by Ond?ej Ry?ka
I use Image.FromFile(string) method in this situation:
我在这种情况下使用 Image.FromFile(string) 方法:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GVEMO
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
PaintBackground();
}
public void PaintBackground() {
gameBoard.Image = Image.FromFile("gvemoBack.jpg");
}
}
}
gameBoard is name for pictureBox. But when I started this program, I get exception FileNotFound. Image is in main directory (directory with folders bin,classes,properties etc.) I try copy this image into all project directories but exception remain. In which directory must be this image or what I do wrong? I use VS2012 and .NET framework version 4.5. Thx
gameBoard 是pictureBox 的名称。但是当我启动这个程序时,我得到了异常 FileNotFound。图像位于主目录中(包含文件夹 bin、classes、properties 等的目录)我尝试将此图像复制到所有项目目录中,但仍然存在异常。该图像必须在哪个目录中或我做错了什么?我使用 VS2012 和 .NET 框架版本 4.5。谢谢
采纳答案by jdehaan
The image must be in the current working directory. At startup, this is the directory where you start your application from, this may be the directory where the executable resides (but must not be). Beware that this working directory can change!
图像必须在当前工作目录中。启动时,这是您启动应用程序的目录,这可能是可执行文件所在的目录(但不能是)。请注意,此工作目录可能会更改!
Rather than using a path I would suggest you to use another mechanism. The current working directory can be set to something different if you use a FileOpenDialog
for example. So you would be better off using a resource or a setting for the directory where the images reside.
我建议您使用另一种机制,而不是使用路径。FileOpenDialog
例如,如果您使用 a ,则可以将当前工作目录设置为不同的内容。因此,最好为图像所在的目录使用资源或设置。
回答by aguyngueran
Please make sure the image is in the same directory as the application executable.
请确保图像与应用程序可执行文件位于同一目录中。
回答by Patko
As jdehaan already said, image should (usually) be in the same directory with your executable. Thas is most likely bin\Debug
in your case.
正如 jdehaan 已经说过的,图像应该(通常)与您的可执行文件位于同一目录中。这很可能bin\Debug
是你的情况。
If this is a static image you could also embed it within your assembly. Look at Embedding Image Resources in a Project.
回答by David Heffernan
Since you used a relative path, the image must reside in the working directory. When you start an executable, unless you specify otherwise, the working directory is set to be the directory in which the executable resides. Clearly the image is not there.
由于您使用了相对路径,因此图像必须位于工作目录中。启动可执行文件时,除非另有指定,否则工作目录将设置为可执行文件所在的目录。显然,图像不存在。
Since it can be hard to extert control over the working directory, especially in a GUI app, it is usually better to specify a full absolute path rather than a relative path. For example:
由于很难控制工作目录,尤其是在 GUI 应用程序中,通常最好指定完整的绝对路径而不是相对路径。例如:
string dir = Path.GetDirectoryName(Application.ExecutablePath);
string filename = Path.Combine(dir, @"MyImage.jpg");
However, far better for your scenario would be to include the image as a resource and so make your executable self-contained. Then you don't need to worry about details like making sure the images land in the same directory as the executable.
但是,更适合您的方案是将图像作为资源包含在内,从而使您的可执行文件自包含。然后您无需担心细节,例如确保图像与可执行文件位于同一目录中。
回答by Jurgen Camilleri
Make sure your file is visible in the solution explorer. If not, click the Show All Files
button in the solution explorer, right-click the image, and click `Include In Project".
确保您的文件在解决方案资源管理器中可见。如果没有,请单击Show All Files
解决方案资源管理器中的按钮,右键单击图像,然后单击“包含在项目中”。
If your file is visible, then make sure that the image is being copied to the output directory by right-clicking on the image in the solution explorer, choosing properties, and making sure the Copy to Output Directory
option is set to Copy if newer
or Copy always
.
如果您的文件可见,请通过右键单击解决方案资源管理器中的图像,选择属性,并确保将Copy to Output Directory
选项设置为Copy if newer
或来确保将图像复制到输出目录Copy always
。
回答by Jurgen Camilleri
If you don't specify a path, the current directory is used.
如果不指定路径,则使用当前目录。
You can either use a relative path:
您可以使用相对路径:
./xxx/yyy/file.ext
../../xxx/yyy/file.ext
or an absolute path:
或绝对路径:
/xxx/yyy/file.ext
or change the directory before launching the program or during its execution (language/platform dependant solution)
或在启动程序之前或执行期间更改目录(语言/平台相关解决方案)
回答by asreen fathima
Even i had the same issue.
即使我有同样的问题。
I did this to make it work fine:
我这样做是为了让它工作正常:
If the image files are added directly to the project : 1.Right Click on the image->Properties window->Select "Copy always" or "Copy if newer" from the 'Copy' dropdown menu.
如果图像文件直接添加到项目中: 1. 右键单击图像-> 属性窗口-> 从“复制”下拉菜单中选择“始终复制”或“如果更新则复制”。
That's it.
就是这样。