添加图像文件资源到另一个项目图像文件、项目、资源

2023-09-06 16:52:38 作者:官方认证小可爱

我想添加/更新图片资源在.NET项目,使应用程序编译时图像会在运行时可用。

I want to add/update Image resources in a .NET project, so that when the application is compiled the images will be available at runtime.

由于有大量的在几个地点的图像我想用一个单独的vb.net的项目做到这一点。

Due to having a large amount of images in several locations I want to do this using a separate vb.net project.

我可以看到这样做的唯一方法是将图像文件复制到 [项目] \资源文件夹,然后编辑 [工程] \我的项目\ Resources.resx 来使用这个映像文件,但是这似乎有点哈克,我不知道怎么工作室挑剔的视觉是关于RESX文件的格式。

The only way I can see to do this is to copy the image file to the [Project]\Resources folder and then edit the [Project]\My Project\Resources.resx to use this image file, but this seems a bit hacky and I'm not sure how fussy Visual Studio is about the format of the resx file.

是否有这样做的其他方式?

Is there some other way of doing this?

推荐答案

您可以使用的 ResourceWriter 类以编程方式创建一个二进制资源(的.resources)直接从code文件。

You can use the ResourceWriter class to programmatically create a binary resource (.resources) file directly from code.

    using (ResourceWriter rw = new ResourceWriter(@".\CarResources.resources"))
    {
       Image im = Image.FromFile("C:\\sample.jpg");
        rw .AddResource("sample.jpg", im) 
      }

您还可以使用资源文件生成器器(Resgen.exe)从文本文件或.resx文件创建.resources文件。

You can also use Resource File Generator (Resgen.exe) to create a .resources file from a text file or a .resx file.