Getting Icon from ResourceStream

2019-05-16 09:40发布

问题:

I have a Icon.ico and in the Properties the Build Action is "Resource"...

I want to load that Icon in the Application..

I did something like this:

Icon theIcon = new Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.Icon.ico"));

that didnt worked (it says "Value of 'null' is not valid for 'stream'.")

What can I do?

回答1:

try using Application.GetResourceStream method

using(Stream stream = Application.GetResourceStream(new Uri("/MyNameSpace.ico")).Stream)
{
    Icon myIcon = new System.Drawing.Icon(stream);
}

more information from MSDN