[Mono-list] What do I do with my images...

David Anes david.anes at gmail.com
Sun Mar 4 09:24:47 EST 2007


Hi again José:
Ok, let's say you have a file named "MyBitmap.bmp" and you have embedded it
into the assembly.

The firt you need to do is to get a reference to the assembly. If you are
retrieving the image from the same assembly where the code is executing, you
can use:

Assembly as = Assembly.GetExecutingAssembly();

With the assembly, you can inspect what resources you have. The easiest way
is to get a list of files embedded using:

string[] resource_names = as..GetManifestResourceNames();

Then you can get a stream to the embedded resource you want:

Stream s = as..GetManifestResourceStream( res_name )

where "res_name" is a resource name that you got previously. This way you
get a stream to the embedded resource. Now, you can read the stream (or do
whatever you need).

For example:

Bitmap bmp = new Bitmap( as.GetManifestResourceStream( "MyBitmap.bmp") );

You need to take into account the way MonoDevelop and VisualStudio take care
of embeded resources, because the names of the file of the resource does not
always match the name in the resource. MonoDevelop, for example, uses the
name of the resource directly (this way, the latest line of code I've
written is actually valid), but VisualStudio uses the "Default Namespace
Name" in the project to prepent it to the resources. So you can end up with
a "this.is.mi.namespace.MyBitmap.bmp" instead. This is the reason to inspect
resource names before you actually try to get a stream for them.

Another problem you may find, is the way to load back the resources, because
you have a stream, instead a physical file. Librsvg allows you to load
directly from a memory chunk, instead of a file, so try this way instead to
load directly from an on-disk file.

Cheers,
David Anes.




2007/3/4, David Anes <david.anes at gmail.com>:
>
> Hi José.
>
> Sorry to reply only to you, it was clearly a mistake.
>
> It's a bit later here in Spain, so tomorrow I will give you some tips and
> hints so you can get back the images from resources. It is not needed to
> have paths, simply get a stream to the resource a read it back.
>
> Cheers,
> David Anes.
>
> 2007/3/4, José Pablo Fernández <pupeno at pupeno.com>:
> >
> > Hello David,
> > Thank you for your reply, I hope you don't mind that I put the thread
> > back
> > into the mailing list, I believe this can be useful for other people (or
> > myself, in a couple of months when I won't have this mail anymore and
> > it'll
> > be in the archives).
> >
> > On Saturday March 3 2007 16:21, David Anes wrote:
> > > Hi Pablo,
> > > Embed the images into the assembly as resources,
> >
> > I believe I have already done that. I've added the images as deployable
> > resources in MonoDevelop. When I have to pick an icon for example, I can
> > click in the Resources tab and see my images there. Is that enough?
> >
> > > and load back the images at runtime.
> >
> > Can you give me any hints about how this is done?
> > Note: the images are SVG and are loaded my own code (wrapper around
> > Cairo and
> > Rsvg). Having a class/method resolving the absolute path having only a
> > relative path (like "images/blah.svg") would be the best, but if I can
> > get
> > the file as a kind of object, then I'll figure it out how to use that
> > object.
> > I've played a bit with ResourceManager because I understand that is the
> > class
> > for this, but I couldn't get anywhere.
> >
> > Thank you.
> > --
> > José Pablo Fernández <pupeno at pupeno.com> (http://pupeno.com)
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20070304/3d0656af/attachment-0001.html 


More information about the Mono-list mailing list