C# 从解决方案中的文件夹加载图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10673957/
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
Load image from folder in solution?
提问by 3D-kreativ
I'm trying to load an image from a folder in the solution, but I only get a error message that it's not found. What have I done wrong? The code below is in the MainForm.cs that is at the same level as the Resource folder. Help is preciated! Thanks!
我正在尝试从解决方案中的文件夹加载图像,但我只收到一条错误消息,指出它未找到。我做错了什么?下面的代码位于与 Resource 文件夹处于同一级别的 MainForm.cs 中。帮助是宝贵的!谢谢!
// Images
Image imageCircle = Image.FromFile("Resources/circle.png");
// Set deafult picture on start
pictureBox1.Image = imageCircle;
采纳答案by Madurika Welivita
It always take the path from the where executable is located(bin folder). So if you can access it using full path, problem will solved. Or you can have a configuration item for the root folder. then access like Image.FromFile(rootFolder+ "Resources/circle.png");. Anyway this issue wont be there when you deploy it.
它总是从可执行文件所在的位置(bin 文件夹)获取路径。因此,如果您可以使用完整路径访问它,问题就会解决。或者您可以为根文件夹设置一个配置项。然后访问像Image.FromFile(rootFolder+ "Resources/circle.png");。无论如何,当您部署它时,这个问题不会出现。
And if you are using resource file,
如果您使用的是资源文件,
<projectName>.Properties.Resources.<ImageName>;
will return the image.
将返回图像。
回答by Andrius Naru?evi?ius
The program is executed in bin/debug (the place where all .dll's are), put the Resourcesfolder there.
该程序在 bin/debug (所有 .dll 所在的地方)中执行,将Resources文件夹放在那里。
回答by Mark Hall
Edit: Fixed Broken Links
编辑:修复断开的链接
Take a look at this MSDN article, it discusses Adding and Editing Resources and what your options are, and this MSDN articlediscussing Linked and Embedded resources using the Resource Designer.
看看这篇MSDN 文章,它讨论了添加和编辑资源以及您的选项,以及这篇MSDN 文章讨论了使用资源设计器链接和嵌入的资源。


Then select your file
然后选择你的文件


Then you can access it like Madurika suggests.
然后你可以像 Madurika 建议的那样访问它。
i.e.
IE
Image imageCircle = YourPojectName.Properties.Resources.YourFileNameHere;



