You can embed items into your WinForm app. So how to do you figure out what you have and the correct path to retreive the items? Here’s a method to show you what’s an embedded resource in your application.
Code:
using System.Diagnostics;
using System.Reflection;
private void ExposeResources()
{
Assembly assembly = Assembly.GetExecutingAssembly();
foreach ( string resource in assembly.GetManifestResourceNames() )
Debug.WriteLine(resource);
}
Usage:
ExposeResources();
Reference:
http://msdn.microsoft.com/en-us/library/aa287526(VS.71).aspx
I’ve embedded images into my app. Now I need to retrieve those images. Here’s code to do it.
Code:
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
...
Image workingImage;
private void LoadImages()
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream file;
if ( workingImage == null )
{
file = assembly.GetManifestResourceStream(".WorkingAni_32x32.gif");
if (file != null)
{
workingImage = Image.FromStream(file);
}
}
}
Usage:
Change the <AssemblyName> to match your environment. If you can't find the embedded resouce, use my "List Embedded Resources" snippet to find the correct path to your image.
The build action for the image properties must be set to 'Embedded Resource'.
If you are adding a new image, the project MUST BE REBUILT before the image is embedded. Rebuilding the entire solution is an easy way to ensure this gets done.