0

How to get a particular image (as a Stream) from C# desktop application's \Resources folder?

In Visual Studio, I imported the image into the \Resources folder and it has a File Name and a Full Path property. I would like to use its name.

My goal is to do this:

System.IO.Stream contentStream =  get the image stream from \Resources
System.Net.Mail.LinkedResource logo = new LinkedResource( contentStream);
Prix
  • 19,417
  • 15
  • 73
  • 132
Tim
  • 8,669
  • 31
  • 105
  • 183
  • At this point, I do not understand this documentation, specifically how type `Example` is involved, and where the resource name is: https://msdn.microsoft.com/en-us/library/zxee5096(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-3 – Tim Dec 14 '15 at 19:16
  • I assume `Example` just uses the current class to find the assembly, but where are they getting the name "SplashScreen"? – Tim Dec 14 '15 at 19:19

1 Answers1

4

You have a couple of simple options.

The /Resources folder is not exactly a special folder. You still have to select any item in the Solution Explorer that you want embedded into the assembly, and then, from the Properties window, specify that its build type is "Embedded Resource". Then use ResourceManager.GetStream to retrieve the contents as a stream.

The simpler method for most cases, though, is to use the Project Properties Designer window to add a Resources.resx file, then add the files you want to the Resources designer. For example, you can add images like this:

enter image description here

Visual Studio will generate code for you to retrieve the files you embed this way.

Alan McBee
  • 4,202
  • 3
  • 33
  • 38
  • Thanks. I am stuck at this point trying to instantiate a ResourceManager. What is the default "baseName", the first parameter supplied to the constructor? If I go into Project Properties -Resources, I can see the embedded images there. – Tim Dec 14 '15 at 19:27
  • I am getting this error: {"Could not find any resources appropriate for the specified culture or the neutral culture. Make sure \"Resources.resources\" was correctly embedded or linked into assembly \"foo\" at compile time, or that all the satellite assemblies required ... – Tim Dec 14 '15 at 19:35
  • I often have to mess around when I take the first approach of using the ResourceManager directly. Read https://support.microsoft.com/en-us/kb/319292 for more help on that. I much prefer to use the Resources designer for most cases; it's much easier to manage, and you don't need to worry about getting the names right; VS does it for you. If you want to do it by yourself anyway, use the Resources designer, then look in the generated Resources.Designer.cs file to see how it constructs the ResourcesManager. – Alan McBee Dec 14 '15 at 19:45
  • Thanks, I looked in the Resources.Designer.cs file and now making some progress. – Tim Dec 14 '15 at 19:56