How do you access resource files (bmp, mp3, etc) that are compiled into your Windows Forms executable? After googling the subject, the Assembly object's GetManifestResourceStream method seemed to be the solution. MSDN's entry on this subject makes it seem relatively simple. However, it took me over an hour to get it to work in my latest project. There are two things you must do in other to access an executable's resources:
You must know the exact name of resource (mynamespace.resource.resourcename).
You must embed the resource into your executable.
Accessing the resources means that you must have access to the executable's Assembly object. I cached this in the Form's Load event:
Assembly _assembly; _assembly = Assembly.GetExecutingAssembly();
The image file that I was trying to access in my program was called Image1.bmp. I made a call to GetManifestResourceSteam like this:
imageStream = _assembly.GetManifestResourceStream(
"ThumbnailPictureViewer.Image1.bmp");
The problem was that _imageStream was always returning NULL. Digging a little further, various articles mentioned that my decorated name might be incorrect. Perhaps I was using the wrong namespace, or the case sensitive name might be wrong? The only way to determine this was to use GetManifestResourceNames(). You can retrieve a collection of string resource names and dump them out to the console:
string[] names = _assembly.GetManifestResourceNames(); foreach (string name in names) System.Console.WriteLine(name);
The console showed me a few resource namespaces, but no embedded objects! Even had Image1.bmp clearly in my Solution Explorer, and in my Project properties, Resources tab, it never showed up. The trick in getting this to work is to make certain that Image1.bmp was set to Embedded Resource in the Properties window:
When you create resources in Visual Studio 2005, they are linked to your executable by default at compile time. This is a nice option if you want to keep the resource files separate from the .resx file, allowing other people to change them. If it needs to be accessed programatically with GetManifestResourceStream, it has to be Embedded into the .resx file. The only drawback here is that my Image resource can only be changed within Visual Studio 2005. Once I changed the Build Action to Embedded, I saw the correct name in the output window:
ThumbnailPictureViewer.resources.Image1.bmp
And then my code to load Image1.bmp into a Bitmap object worked successfully:
Stream _imageStream;
_imageStream =
_assembly.GetManifestResourceStream(
"ThumbnailPictureViewer.resources.Image1.bmp");
Bitmap theDefaultImage = new Bitmap(_imageStream);
External Link:
MSDN summary on Linked vs. Embedded resources.




