使用c#= FileNotFoundException更改pictureBox.Image

时间:2021-02-22 00:24:41

i have a small app in C# winform. It work great but i don't understand how to change the image of a picture box in code :

我在C#winform中有一个小应用程序。它工作得很好,但我不明白如何在代码中更改图片框的图像:

i have this directory for my image :

我有我的图像的目录:

myProjectDirectory/bin/Pics/myImage.jpg

myProjectDirectory /斌/照片管理/ myImage.jpg

and this code give me an FileNotFoundException :

这段代码给我一个FileNotFoundException:

this.imgInvader.Image = Image.FromFile("../Pics/invader2.jpg");

i don't understand because i see on * that FromFile method begin at bin/Release. So a ../Pics/myImage.jpg should work no ?

我不明白,因为我在*上看到FromFile方法从bin / Release开始。那么../Pics/myImage.jpg应该不行吗?

Thx

谢谢

3 个解决方案

#1


1  

Use the relative path of the image.

使用图像的相对路径。

this.imgInvader.Image = Image.FromFile(@"bin\Pics\invader2.jpg");

Here give the path from the location where your code behind file is located. Suppose if your file is in root directory and if your images are in bin/Pics/ folder then the above code works. It automatically gets the path related to the location the program is running from.

这里给出了代码隐藏文件所在位置的路径。假设您的文件位于根目录中,如果您的图像位于bin / Pics /文件夹中,则上述代码可以正常工作。它会自动获取与程序运行位置相关的路径。

#2


1  

Trying to reference image files that are outside the executable output directory is incredibly fragile. There are lots of ways it can go wrong (unfortunately, there's not enough context in your question for anyone to know exactly which of these ways is your specific problem).

尝试引用可执行输出目录之外的图像文件非常脆弱。有很多方法可以出错(不幸的是,你的问题没有足够的背景让任何人确切地知道这些方法中的哪一个是你的具体问题)。

If you must use files on disk to store your image resources, then they should be copied into the build output directory (i.e. "Release") and referenced there. Add the file to your Visual Studio project, select it, and in the properties window, set the "Build Action" value to "Content". If the file is in a folder under the project, then it will also be copied to a folder of the same name in the output directory.

如果必须使用磁盘上的文件来存储映像资源,则应将它们复制到构建输出目录(即“发布”)并在那里引用。将文件添加到Visual Studio项目中,选择它,然后在属性窗口中将“Build Action”值设置为“Content”。如果文件位于项目下的文件夹中,则它也将被复制到输出目录中的同名文件夹中。

If you do use files on disk, the other thing to make sure you do is find the executable's directory (e.g. via Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)) and then combine that with your expected relative path (e.g. just the file name, or the file name under whatever folder/subdirectory you gave it in the project, if you did) using the Path.Combine() method, and then using that absolute file name as the source. Otherwise, your code can be confused by changes in the current directory made elsewhere in your program (basically, don't ever rely on the current directory…global state like that is too easy to get mixed up, once you get into the habit of using it).

如果你确实在磁盘上使用文件,那么确保你做的另一件事就是找到可执行文件的目录(例如通过Path.GetDirectoryName(Assembly.GetEntryAssembly()。Location)),然后将它与你预期的相对路径(例如文件名,或者在项目中给出它的任何文件夹/子目录下的文件名,如果你这样做的话)使用Path.Combine()方法,然后使用该绝对文件名作为源。否则,您的代码可能会被程序中其他位置的当前目录中的更改所迷惑(基本上,不要依赖于当前目录......这样的全局状态太容易混淆了,一旦您养成了习惯,使用它)。

For example:

例如:

string exeDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string imageFileName = Path.Combine(exeDirectory, "invader2.jpg");

Now, all that said, IMHO it is probably a better idea to add your images as resources in the executable itself, and then reference them from the Properties.Resources class. Then the images are always with the executable, because they are in the same file. The code is a lot easier too, because you're just referencing properties in the Resources class that return the actual Image objects you need.

现在,所有这些,恕我直言,将图像作为资源添加到可执行文件本身,然后从Properties.Resources类引用它们可能是一个更好的主意。然后,图像始终与可执行文件一起使用,因为它们位于同一文件中。代码也更容易,因为您只是引用Resources类中的属性,它返回您需要的实际Image对象。

#3


0  

As the previous comment stated, using resources outside of your exe is not advised however you can still do this by using the Path.GetDirectoryName Method.

如前面的评论所述,不建议使用exe之外的资源,但仍可以使用Path.GetDirectoryName方法执行此操作。

I am left to question why your resources are based outside of your exe, why not embed it into your resources located in properties > resources.resx and simply call it with imgInvader.Image = Properties.Resources.FileNamehere; it is a lot safer than trusting the external environment.

我不得不质疑为什么你的资源是在你的exe之外,为什么不将它嵌入你的属性> resources.resx中的资源,只需用imgInvader.Image = Properties.Resources.FileNamehere调用它;它比信任外部环境更安全。

#1


1  

Use the relative path of the image.

使用图像的相对路径。

this.imgInvader.Image = Image.FromFile(@"bin\Pics\invader2.jpg");

Here give the path from the location where your code behind file is located. Suppose if your file is in root directory and if your images are in bin/Pics/ folder then the above code works. It automatically gets the path related to the location the program is running from.

这里给出了代码隐藏文件所在位置的路径。假设您的文件位于根目录中,如果您的图像位于bin / Pics /文件夹中,则上述代码可以正常工作。它会自动获取与程序运行位置相关的路径。

#2


1  

Trying to reference image files that are outside the executable output directory is incredibly fragile. There are lots of ways it can go wrong (unfortunately, there's not enough context in your question for anyone to know exactly which of these ways is your specific problem).

尝试引用可执行输出目录之外的图像文件非常脆弱。有很多方法可以出错(不幸的是,你的问题没有足够的背景让任何人确切地知道这些方法中的哪一个是你的具体问题)。

If you must use files on disk to store your image resources, then they should be copied into the build output directory (i.e. "Release") and referenced there. Add the file to your Visual Studio project, select it, and in the properties window, set the "Build Action" value to "Content". If the file is in a folder under the project, then it will also be copied to a folder of the same name in the output directory.

如果必须使用磁盘上的文件来存储映像资源,则应将它们复制到构建输出目录(即“发布”)并在那里引用。将文件添加到Visual Studio项目中,选择它,然后在属性窗口中将“Build Action”值设置为“Content”。如果文件位于项目下的文件夹中,则它也将被复制到输出目录中的同名文件夹中。

If you do use files on disk, the other thing to make sure you do is find the executable's directory (e.g. via Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)) and then combine that with your expected relative path (e.g. just the file name, or the file name under whatever folder/subdirectory you gave it in the project, if you did) using the Path.Combine() method, and then using that absolute file name as the source. Otherwise, your code can be confused by changes in the current directory made elsewhere in your program (basically, don't ever rely on the current directory…global state like that is too easy to get mixed up, once you get into the habit of using it).

如果你确实在磁盘上使用文件,那么确保你做的另一件事就是找到可执行文件的目录(例如通过Path.GetDirectoryName(Assembly.GetEntryAssembly()。Location)),然后将它与你预期的相对路径(例如文件名,或者在项目中给出它的任何文件夹/子目录下的文件名,如果你这样做的话)使用Path.Combine()方法,然后使用该绝对文件名作为源。否则,您的代码可能会被程序中其他位置的当前目录中的更改所迷惑(基本上,不要依赖于当前目录......这样的全局状态太容易混淆了,一旦您养成了习惯,使用它)。

For example:

例如:

string exeDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string imageFileName = Path.Combine(exeDirectory, "invader2.jpg");

Now, all that said, IMHO it is probably a better idea to add your images as resources in the executable itself, and then reference them from the Properties.Resources class. Then the images are always with the executable, because they are in the same file. The code is a lot easier too, because you're just referencing properties in the Resources class that return the actual Image objects you need.

现在,所有这些,恕我直言,将图像作为资源添加到可执行文件本身,然后从Properties.Resources类引用它们可能是一个更好的主意。然后,图像始终与可执行文件一起使用,因为它们位于同一文件中。代码也更容易,因为您只是引用Resources类中的属性,它返回您需要的实际Image对象。

#3


0  

As the previous comment stated, using resources outside of your exe is not advised however you can still do this by using the Path.GetDirectoryName Method.

如前面的评论所述,不建议使用exe之外的资源,但仍可以使用Path.GetDirectoryName方法执行此操作。

I am left to question why your resources are based outside of your exe, why not embed it into your resources located in properties > resources.resx and simply call it with imgInvader.Image = Properties.Resources.FileNamehere; it is a lot safer than trusting the external environment.

我不得不质疑为什么你的资源是在你的exe之外,为什么不将它嵌入你的属性> resources.resx中的资源,只需用imgInvader.Image = Properties.Resources.FileNamehere调用它;它比信任外部环境更安全。