Image as resource in Asp.Net 5 class library

2019-02-22 10:20发布

问题:

In .net 4.5 i was able to add a resource file to my project, add images as bitmaps to this resource file and access them by Properties.Resources.ImageName. How do i compile images in 4.6 dnx?

Thanks in advance!

回答1:

You can specify the files that will be compiled into the assembly under the "resources" section in project.json, like so:

"resources": [
    "path/to/yourfile.png"
],

After which, assuming your project's name is YourProject, the file can be accessed via:

const string path = "YourProject.path.to.yourfile.png";
Assembly.GetExecutingAssembly().GetManifestResourceStream(path)

Note how the slashes in the filesystem path are converted into periods in the resource path.

Note: In 1.0.0-rc1 (and possibly -beta8, haven't checked) the project setting is renamed from resources to resource