Home All Groups Group Topic Archive Search About

Embedding an image in a dll

Author
10 Mar 2006 4:59 PM
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.

Author
10 Mar 2006 5:18 PM
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...





Show quoteHide quote
"UJ" <f***@nowhere.com> wrote in message
news:%23EFivPGRGHA.1688@TK2MSFTNGP11.phx.gbl...
> 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.
>
>
Are all your drivers up to date? click for free checkup

Author
10 Mar 2006 6:36 PM
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.

Show quoteHide quote
"sklett" <skl***@mddirect.com> wrote in message
news:OMa1fZGRGHA.2300@TK2MSFTNGP11.phx.gbl...
> 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...
>
>
>
>
>
> "UJ" <f***@nowhere.com> wrote in message
> news:%23EFivPGRGHA.1688@TK2MSFTNGP11.phx.gbl...
>> 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.
>>
>>
>
>
Author
10 Mar 2006 6:47 PM
Ignacio Machin ( .NET/ C# MVP )
Hi,

"UJ" <f***@nowhere.com> wrote in message
news:%23KLaOGHRGHA.196@TK2MSFTNGP10.phx.gbl...
> 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


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Bookmark and Share