Embedding an image in a dll

U

UJ

I've got an image I want to embed in a dll to use on a screen later. I've
got it in a resource file, got it to compile in to the dll. The problem is
getting it back out. It seems like my problem is in the get resource code.

The code I'm using is:


System.Reflection.Assembly myAssembly;
myAssembly = this.GetType().Assembly;

// Creates the ResourceManager.
System.Resources.ResourceManager myManager = new
System.Resources.ResourceManager("Marlin.VisualControls",
myAssembly);

// Retrieves Image resource.
System.Drawing.Image myImage;
myImage =
(System.Drawing.Image)myManager.GetObject("ECSStartupGraphic");

I can't tell whether it's the resource manager that is set up wrong or the
get object is wrong (I have a feeling the resource manager is wrong.)

The namespace that the resource is in is Marlin.VisualControls. The name of
the object is ECSStartupGraphic (I verified this through resource editor and
also by looking at the file.)

I've also added the file to the project and set it's Build Action as
Embedded Resource.

I just can't seem to get the references right.

And if I've got a window with this image on it (it will always be this
image) is there someway to tell it the file is a reference instead of trying
to look at the disk somewhere?

TIA - Jeff.
 
S

sklett

Here's what I've cooked up:

string samplePath = "Modules.Inventory.Resources.WI_ReviewIcon.png";

Image img = GetImageFromManifest(samplePath);


/// <summary>
/// Convert an embedded image resource to a Drawing.Image object
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public Image GetImageFromManifest(string path)
{
Image newImage = null;
try
{
Assembly thisAssembly = Assembly.GetAssembly(this.GetType());
Stream resourceStream = thisAssembly.GetManifestResourceStream(path);
newImage = Image.FromStream(resourceStream);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
return newImage;
}



Hope that helps...
 
U

UJ

That worked great. Thanks. I think my main problem was not knowing the name
of the item to use but I figured it.

Jeff.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

UJ said:
That worked great. Thanks. I think my main problem was not knowing the
name of the item to use but I figured it.

You need to use the complete name of the object, including the namespace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top