Home
Manage Your Code
Snippet: Get Embedded Image from Assembly (C#)
Title: Get Embedded Image from Assembly Language: C#
Description: Gets an embedded image from the current assembly. Embedded images are found in the assembly manifest. Views: 138
Author: Dave Donaldson Date Added: 1/21/2008
Copy Code  
1using System.Reflection;
2using System.IO;
3
4/// <summary>

5/// Gets an embedded image from the current assembly.

6/// </summary>

7/// <param name="imagePath">Full namespace path of the image.</param>

8/// <returns>The image found at <paramref name="imagePath"/>.</returns>

9/// <example>

10/// The root namespace of your project is MyCompany.MyApp and you have

11/// an image named foo.png in a folder named Images. The imagePath would

12/// be MyCompany.MyApp.Images.foo.png.

13/// </example>

14public Image GetEmbeddedImage(string imagePath)
15{
16    // Embedded items are found in the assembly manifest

17    Assembly thisAssembly = Assembly.GetAssembly(this.GetType());
18    Stream resourceStream = thisAssembly.GetManifestResourceStream(imagePath);
19    return Image.FromStream(resourceStream);
20}
21
Usage
Image image = GetEmbeddedImage("MyCompany.MyApp.Images.foo.png");