How to add resources in separate folders?

后端 未结 2 1787
醉酒成梦
醉酒成梦 2021-02-01 10:36

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

2条回答
  •  庸人自扰
    2021-02-01 10:57

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

    Update: more in detail:

    1. 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)
    2. 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).

    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: ... You can load an image using this code:

    using(Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(".."))
    {
        pictureBox1.Image = Image.FromStream(stream);
    }
    

提交回复
热议问题