If you use images in a .NET application, chances are you will find it more convenient to embed those images as resources in your project, rather than leaving them as separate files and trying to locate and load the images from disk when the application runs.
Add Image Resource
To add an image to your project as an embedded resource:
- In Visual Studio, click the Project menu, and select Add Existing Item. Find and select the image you want to add to your project.
- In the Solution Explorer window, right-click the image file you just added to your project, and select Properties from the popup menu. The Properties tool window appears.
- In the Properties window (see picture below), change the Build Action property to Embedded Resource.
- Build the project. The image will be compiled into your project’s assembly.
Load Image Resource
Be sure to include the following namespaces in your project:
using System.IO; using System.Reflection;
To load the image resource programmatically, use the following code:
Assembly myAssembly = Assembly.GetExecutingAssembly(); Stream myStream = myAssembly.GetManifestResourceStream( "MyNamespace.SubFolder.MyImage.bmp" ); Bitmap bmp = new Bitmap( myStream );
Resource Path
The trickiest part of loading an embedded resource is getting the correct path. A resource path takes this form:
<namespace>.<subfolders>.<image name>.<extension>
where:
- namespace is the namespace for the project
- subfolders is the folder path within the project, with each folder separated by a period instead of a slash
- image name is the name of the image file
- extension is the image file extension (for example, “bmp” or “jpg”)
Important: Unlike Windows file paths, embedded resource paths are case sensitive.
For example, the About24.png image file is stored in the ArtA subfolder under the main .NET development project. In this case, the path would be “MyNamespace.Art.A.About24.png”.
Show All Embedded Resources
If you’re having trouble determining the correct path, you can always add the following code to show the paths of all embedded resources:
Assembly myAssembly = Assembly.GetExecutingAssembly(); string[] names = myAssembly.GetManifestResourceNames(); foreach (string name in names) { Console.WriteLine( name ); }
Hey, watch the hyphens “-” in folder names, they become underscores “_”. This only happens on folder names not files(go figure!)
Hi, i need some help, i’m integrating several .rtf’s in a form, and i want to put them as embeded resources, but the matter is to call an archive, with the method richtextboxLoadFile() because it accepts a string with the path, and i don’t know how to get it; i saw your example but always comes out an error of File not found (the file exists in the resources folder) it’s necessary declare the assembly??
You cannot get a path for embedded resources, as these files are within your assembly and not stored in a separate file on disk.
I’m not sure what your richtextboxLoadFile() routine does, but it won’t work with embedded resources if you require a file name.
Instead, you need to modify your routine to accept a string argument that contains the file’s CONTENTS. e.g., if you were setting a RichTextBox directly, you would set the .Rtf property.
Is there a way to modify the resource in runtime?
Let’s say that I have a xml file embedded, I open it from my appplication and then I modify the info. How can I update the embedded version of the xml file?
Very useful article.
you rock, man!!!
For my C# project, I put the release .dll on another computer. This application launches ESRI ArcMap. Once open the user adds my application from the View –>Toolbars menu.
The problem is that the menu icons do not display. The icons appear fine on my computer.
From this post it looks like I need to add the code from the “Load Image Resource” section, but where in my code do I put it?
Thanks for your help!
Hi,
Is it work in app to Windows CE ??
Thanks
Very excellent article, you go the distance in adding useful screenshots and explaining things thoroughly, as well as pointing out some caveats. For the first time I feel comfortable loading embedded resources from the code-behind. =) Thanks!
Thanks a lot! very useful :).. i couldnt find this info anywhere else..atleast as far as i could search
Exactly what I was looking for to solve the problem with getting empty file by using MemoryStream.
Great! Thanks a lot!
Very-very good and helpful
In my project at this line ,
Icon = new Icon(this.GetType(), “TextPad.TextPad.Images.NOTEPAD.ICO”);
I get exception ArgumentException was unhandled.
Resource ‘TextPad.TextPad.Images.NOTEPAD.ICO’ cannot be found in class ‘TextPad.TextPad’
@Renuka: Add the “Show All Embedded Resources” code from above to your program right before your line “Icon = new Icon(…” This will show you whether the resource you want is properly embedded in your assembly and what the resource should be named.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
namespace ConsoleApplication19
{
class Program
{
static void Main(string[] args)
{
Assembly Myassembly = Assembly.GetExecutingAssembly();
Stream Mystreem = Myassembly.GetManifestResourceStream(“ConsoleApplication19.happy_friendship_day_images.bmp”);
Console.ReadLine();
}
}
}
sir i have executed the code in console application
but i am getting a blank black screen on my console window
can you haelp me
and i have done all the steps you had menssioned above