如何在单独的文件夹中添加资源

如何在单独的文件夹中添加资源

本文介绍了如何在单独的文件夹中添加资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试通过单击添加现有项目"在资源设计器中添加资源时,该项目将放置在资源"文件夹中.

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".

问题是,如果我在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.

我不能将所有资源都放在一个文件夹中,因为我必须添加2500张图像,其中一些图像与它们的名称匹配.

I can't put all resources in one folder,because I have to add 2500 images and some of them match their names.

推荐答案

您不需要将图像添加到Resources文件夹下.您可以将图像添加到所需的任何文件夹,然后将图像的生成操作设置为嵌入式资源".这样,它们将作为资源编译到程序集中.我不知道当图片很多时是否会出现性能问题...

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...

更新:更详细:

  1. 将文件夹和图像文件作为项目项添加到项目中(以便您可以在解决方案资源管理器中查看每个文件夹及其中的图像)
  2. 将每个图像文件的构建操作"属性设置为嵌入式资源"(您可以同时对多个文件执行此操作;只需在解决方案资源管理器中选择所有图像文件).

这将导致图像文件作为资源编译到程序集中.将按照以下模式为每个文件分配一个资源名称:<root namespace for the assembly>.<folder name>.<image file name>.您可以使用以下代码加载图像:

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:

using(Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("<root namespace for the assembly>.<folder name>.<image file name>"))
{
    pictureBox1.Image = Image.FromStream(stream);
}

这篇关于如何在单独的文件夹中添加资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:06