Search code examples
c#imagewinformsload

Load image from resources area of project in C#


I have an image in my project stored at Resources/myimage.jpg. How can I dynamically load this image into Bitmap object?


Solution

  • The best thing is to add them as Image Resources in the Resources settings in the Project. Then you can get the image directly by doing Resources.myimage. This will get the image via a generated C# property.

    If you just set the image as Embedded Resource you can get it with:

    string name = "Resources.myimage.jpg"
    string namespaceName = "MyCompany.MyNamespace";
    string resource = namespaceName + "." + name;
    Type type = typeof(MyCompany.MyNamespace.MyTypeFromSameAssemblyAsResource);
    Bitmap image = new Bitmap(type.Assembly.GetManifestResourceStream(resource));
    

    Where MyTypeFromSameAssemblyAsResource is any type that you have in your assembly.