Shared Project Embedded Bitmaps

How to load bitmap images from embedded resources in a shared Visual Studio C# project

I recently created a Visual Studio shared project so that I could share source code between several projects I am working on. In the past I had created class libraries to share code between multiple projects. The shared project approach differs from the class library in that rather than sharing compiled code it shares the source code. This approach permits using conditional compilation so that you can target different platforms. I had the need to share several bitmap images as well as source code so I included them in the shared project. I marked them as embedded resources so that they would be compiled into each project that references the shared project. The next question was how to reference and load the embedded bitmap resources. Here's what I cam up with.

To embed images in a shared project as resources you need to follow these steps.

  • Add the image to the shared project by right clicking on the project and choosing add/existing item. I like to create a folder in the project for the images, but that isn't required.
  • Select the propertied for the bitmap image and change the build action to embedded resource

Now that the image(s) are embedded as resources, I we need to be able to reference and load those images. We can reference them by name, but we need the full path rather than just the resource name. So we need a way to convert the image file name to the full path to the resource.

The first thing I did was to find a way to list the full path name for all of the embedded resources. That turned out to be reasonably easy. The following code gets the full path names as an array of strings.

 
    Assembly myAssembly = Assembly.GetExecutingAssembly(); 
    string[] resourceNames = myAssembly.GetManifestResourceNames(); 
    

The above code returns the full path to all of the resources within the compiled executable. I now needed a way to convert from the bitmap file name that the application used to reference the resource to the full resource path name. I did that as follows.


    string resource = "your-image-name-goes-here";
    return Array.Find(resourceNames, delegate (string name) { return name.Contains(resource); }); 
    

The final step was to create a bitmap from the embedded resource. The following code is a class I created that encapsulates everything I needed to be able to load a bitmap from an embedded bitmap resource. It includes the code snippets from above.


    public class ResourceLoader
    {
        // Get a list of embedded resources
        public static string[] ResourceNames
        {
            get
            {
                Assembly myAssembly = Assembly.GetExecutingAssembly();
                return myAssembly.GetManifestResourceNames();
            }
        }

        // Get the full path for a resource name
        public static string GetResourceFullPath(string resource)
        {
            return Array.Find(ResourceNames, delegate (string name) { return name.Contains(resource); }); 
        }

        // Get the embedded resource as a bitmap
        public static Bitmap LoadBitmapResource(string bitmapName)
        {
            Bitmap result = null;

            string fullPath = GetResourceFullPath(bitmapName);
            if (!string.IsNullOrEmpty(fullPath))
            {
                Assembly myAssembly = Assembly.GetExecutingAssembly();
                using (Stream myStream = myAssembly.GetManifestResourceStream(fullPath))
                {
                    result = new Bitmap(myStream);
                }
            }

            return result;
        }
    }
    

I hope you find this helpful.

Written by KB3HHA on 01/03/2022