Embedded image in a Class Library

G

Guest

Hello guys,

C#, VS2005: I'm trying to send an email containing an image.
I use "new AlternateView(imgStream, Mime.MediaTypeNames.Image.Gif);" to do
so, fine.

The problem is before this, when I try to create the Strem (imgStream) from
an embedded resource (myImage) I have in a resource file (myResources.resx).

I tried this code but it always returns "null", if you have any idea, will
be pretty welcome!

Stream imgStream =
Assembly.GetExecutingAssembly().GetManifestResourceStream("myNameSpace.myLibrary.myResources.myImage");

Thanks.
 
A

Andrew Faust

That's because you're asking for the resource from the executing assembly.
The executing assembly is NOT your dll. It's the program that is calling
your dll. From within your dll you should be able to access your resource
directly by name without the Assembly.* stuff. So to use your example you
can call myResources.myImage directly. It will return it as a byte array,
but at that point it's trivial to create a MemoryStream object that
references it.
 
G

Guest

Thanks for your reply, Andrew!

I can get to the bitmap using myAssembly.myResources.myImage as a
System.Drawing.Bitmap
But that's all I can get... I didn't find a way to get the byte[], and much
less a stream out of it...


--
____________________________
Carlos Sosa Albert


Andrew Faust said:
That's because you're asking for the resource from the executing assembly.
The executing assembly is NOT your dll. It's the program that is calling
your dll. From within your dll you should be able to access your resource
directly by name without the Assembly.* stuff. So to use your example you
can call myResources.myImage directly. It will return it as a byte array,
but at that point it's trivial to create a MemoryStream object that
references it.
 
G

Guest

I found it. You pointed me in the right direction mentioning MemoryStream,
and now it works:

Stream imgStream = new MemoryStream();
MyResources.MyImage.Save(imgStream, System.Drawing.Imaging.ImageFormat.Gif);

Thanks a lot!

--
____________________________
Carlos Sosa Albert


Andrew Faust said:
That's because you're asking for the resource from the executing assembly.
The executing assembly is NOT your dll. It's the program that is calling
your dll. From within your dll you should be able to access your resource
directly by name without the Assembly.* stuff. So to use your example you
can call myResources.myImage directly. It will return it as a byte array,
but at that point it's trivial to create a MemoryStream object that
references it.
 

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