embed and use icons

  • Thread starter Thread starter axis
  • Start date Start date
A

axis

What I want to do is pretty simple: I have a bunch of icons in my
application project (visual studio .net 2003), and I want to embed those in
the executable, and access them through code.

I've spent an hour browsing MSDN, and I still don't have a clear picture of
how to do it. Documentation talks about ResourceManagers and other stuff,
but as far as I can tell it talks about creating a dll, not in the
executable itself.

Thanks in advance.
 
Your icons should be included in your project and their Build Action should
be Embedded. With this configuration, it will be included in your assembly
(of your project) , be it a referenced class library (.dll) or your main
application (.exe). From code, you have to use ResourceManager,
particularly the ResourceReader to obtain the said icon from the assembly.

hth
 
I apologize for being thick about this... I keep reading the docs for the
classes and can't make any progress with it. As far as I've determined, my
code should look something like this:

ResourceManager rm = new
ResourceManager("???",Assembly.GetExecutingAssembly());
Icon theIcon = (Icon)rm.GetObject("IconName.ico");

So what goes in the part that says ??? . Lets say project is called
TestProject, and the form in question is Form1. I noticed in obj there was a
file called TestProject.Form1.resources, so I've tried putting
"TestProject.Form1.resources" where the questionmarks are, and the code goes
through, but the next line doesn't retrieve the icon.

Any tips would be appreciated.
 
axis said:
What I want to do is pretty simple: I have a bunch of icons in my
application project (visual studio .net 2003), and I want to embed those
in the executable, and access them through code.

I've spent an hour browsing MSDN, and I still don't have a clear picture
of how to do it. Documentation talks about ResourceManagers and other
stuff, but as far as I can tell it talks about creating a dll, not in the
executable itself.

The following should set get you going. This example is with PNG files set
as Embedded Resources. You will need to modify it to work with icons.

string[] res = Assembly.GetExecutingAssembly().GetManifestResourceNames();
foreach(string s in res)
{
Stream stream =
Assembly.GetExecutingAssembly().GetManifestResourceStream(s);
Image image = Image.FromStream(stream);
}
 
axis said:
What I want to do is pretty simple: I have a bunch of icons in my
application project (visual studio .net 2003), and I want to embed those in
the executable, and access them through code.

I've spent an hour browsing MSDN, and I still don't have a clear picture of
how to do it. Documentation talks about ResourceManagers and other stuff,
but as far as I can tell it talks about creating a dll, not in the
executable itself.

Thanks in advance.

Took me forever to figure it out as well. I've gotta tell you, the .NET docs
pretty much suck. All the examples seem to be generally the same thing, and do
a lousy job of demonstrating what is actually needed.

Forget all the resource manager junk, streams, etc. It all comes down to (from
within your form):

Icon icon = new Icon(GetType(), "myIcon.ico");

The GetType() is the easiest way to get the assembly for your exe, when calling
from within your form.

Other notes: if you store your icons in a subfolder, you will need to prefix
the icon name w/ the subfolder + ".", such as "Icons.myIcon.ico", presuming
they are stored in a subfolder called "Icons" off of your main project folder.

Lastly, make sure that your icon build action is set to "Embedded Resource"
(icon properties, Build Action).
 
Julie said:
Took me forever to figure it out as well. I've gotta tell you, the .NET
docs
pretty much suck. All the examples seem to be generally the same thing,
and do
a lousy job of demonstrating what is actually needed.

Forget all the resource manager junk, streams, etc. It all comes down to
(from
within your form):

Icon icon = new Icon(GetType(), "myIcon.ico");

The GetType() is the easiest way to get the assembly for your exe, when
calling
from within your form.

Other notes: if you store your icons in a subfolder, you will need to
prefix
the icon name w/ the subfolder + ".", such as "Icons.myIcon.ico",
presuming
they are stored in a subfolder called "Icons" off of your main project
folder.

Lastly, make sure that your icon build action is set to "Embedded
Resource"
(icon properties, Build Action).

That worked! Thank you so much.
 

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

Back
Top