Home > Technology > Load an embedded resource

Load an embedded resource

November 24th, 2009 Leave a comment Go to comments

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.
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.