C# 如何在单独的文件夹中添加资源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/859699/
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
How to add resources in separate folders?
提问by Ivan Prodanov
When I try to add a resource at the resource designer by clicking "Add an existing item",the item is placed in the folder "Resource".
当我尝试通过单击“添加现有项目”在资源设计器中添加资源时,该项目被放置在“资源”文件夹中。
The problem is that if I create a new directory in the Resource directory and place the resources there,I get a compiler error that the files cannot be found.
问题是,如果我在 Resource 目录中创建一个新目录并将资源放在那里,我会收到一个编译器错误,提示找不到文件。
I can't put all resources in one folder,because I have to add 2500 images and some of them match their names.
我不能将所有资源放在一个文件夹中,因为我必须添加 2500 张图像,其中一些图像与它们的名称相匹配。
采纳答案by Fredrik M?rk
You do not need to add the images under the Resources folder. You can add the images to any folder you wish, and then set the build action for the images to "Embedded Resource". That way they will be compiled into the assembly as resources. I don't know if there are performance issues coming into play when it is a large number of images though...
您不需要在 Resources 文件夹下添加图像。您可以将图像添加到您希望的任何文件夹,然后将图像的构建操作设置为“嵌入式资源”。这样,它们将作为资源编译到程序集中。我不知道当它是大量图像时是否会出现性能问题......
Update: more in detail:
更新:更详细:
- Add the folders and image files as project items to the project (so that you can see each folder and the images within it in the Solution Explorer)
- Set the Build Action property of each of the image files to "Embedded Resource" (you can do this for multiple files at the same time; just select all the image files in the solution explorer).
- 将文件夹和图像文件作为项目项添加到项目中(以便您可以在解决方案资源管理器中看到每个文件夹及其中的图像)
- 将每个图像文件的 Build Action 属性设置为“Embedded Resource”(您可以同时对多个文件执行此操作;只需在解决方案资源管理器中选择所有图像文件)。
This will cause the image files to be compiled into the assembly as resources. Each file will be assigned a resource name following this pattern: <root namespace for the assembly>.<folder name>.<image file name>
. You can load an image using this code:
这将导致图像文件作为资源编译到程序集中。将按照以下模式为每个文件分配一个资源名称:<root namespace for the assembly>.<folder name>.<image file name>
. 您可以使用以下代码加载图像:
using(Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("<root namespace for the assembly>.<folder name>.<image file name>"))
{
pictureBox1.Image = Image.FromStream(stream);
}
回答by Suneet
Create a new resource file (in following example I called it Images01 in folder resx) Create a custom resource manager class and initialize it to to point to this file just created
创建一个新的资源文件(在下面的示例中,我在文件夹 resx 中将其称为 Images01) 创建一个自定义资源管理器类并将其初始化为指向刚刚创建的这个文件
ResourceManager rm = new ResourceManager("ROOTNAMESPACE.resx.Images01",
System.Reflection.Assembly.GetExecutingAssembly());
Implement the method to GetImage
实现GetImage的方法
public static Image GetImage(string fileName)
{
Stream stream = GetResourceStream(fileName);
Image image = null;
if (stream != null)
{
image = Image.FromStream(stream);
}
return image;
}
Add images to this resx file
将图像添加到此 resx 文件
And then you can use it in your code as follows
然后你可以在你的代码中使用它,如下所示
this.picProject.Image = Resources.GetImage("ImageName.png");
Hope it helps
希望能帮助到你