473,748 Members | 5,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a resource file for icons

Hello all:

I am trying to build a resource dll that will house icons, and I'm afraid
that I'm not having much luck reading it. Can you lend hand (or just point
me to a good primer).

Here's what I've done so far: (VS.NET 2003/C#)

1. Created an empty project for the resource dll. Output type is class
library.
2. Added a bunch of icon files to the project (add existing item).
3. Set the build action of each icon file to "embedded resource".
4. So, this project is of type class library, has no references, and only
contains .ico files, no .cs files.

Next, in a separate test exe, I run the code below to try to simulate
reading the resources. I get an ArgumentExcepti on in the constructor of the
ResourceReader: "Stream is not a valid resource file". (the code is below)

I think that either I am not building the resource dll correctly, or I am
not using the right resource class to try to read it. Any hints?
private void ReadResources(s tring resourceFileNam e) // resourceFileNam e ==
"myResources.dl l"
{
System.Resource s.ResourceReade r reader = new
System.Resource s.ResourceReade r(resourceFileN ame);
try
{
System.Collecti ons.IDictionary Enumerator enumerator =
reader.GetEnume rator();
while(enumerato r.MoveNext())
System.Diagnost ics.Debug.Write Line(enumerator .Key);
}
finally
{
reader.Close();
}

}

Nov 16 '05 #1
3 10381
J.Marsch wrote:
Hello all:

I am trying to build a resource dll that will house icons, and I'm afraid
that I'm not having much luck reading it. Can you lend hand (or just point
me to a good primer).


I was just stuck on this too. I solved it by using a .resources file
which I linked in to the application the same way you mention in your
description (by setting that 'embeddable' flag on).

You can call it with the ResourceManager class. The string you pass into
the constructor is the name of the resources file without the suffix,
and you have to make sure that if you have an application namespace you
prepend it to the name.

So if my resources file was "myapplication. resources" and my application
namespace set in Visual Studio was "mynamespac e" I would pass in
"mynamespace.my application" into the constructor. You can then just call
..GetResourceSe t on it.

I hope that helps a little.

Regards,
Dustin Aleksiuk
Nov 16 '05 #2
J.Marsch wrote:
Hello all:

I am trying to build a resource dll that will house icons, and I'm afraid
that I'm not having much luck reading it. Can you lend hand (or just point
me to a good primer).

Here's what I've done so far: (VS.NET 2003/C#)

One more thing: you can use ILDasm.exe to check your .DLL and see if the
resources file got linked in properly. It will also tell you if there is
a namespace you need in order to find it.

Hope I'm not out to lunch with this, but it worked for me yesterday.

Regards,
Dustin Aleksiuk
Nov 16 '05 #3
Thanks for your reply, Dustin. I think that I found an additional way to do
it (would love to hear from anyone whether this is the best way):

To get the list of resources (note: this ugly test code is using a Listbox
to display the resource names):
// this code works with a resource dll.
private void ReadResourcesFr omManifest(stri ng resourceFileNam e)
{
this.ResourcesL istBox.Items.Cl ear();
this.TargetAsse mbly =
System.Reflecti on.Assembly.Loa dFile(resourceF ileName);
this.ResourcesL istBox.Items.Ad dRange(this.Tar getAssembly.Get ManifestResourc eNames());
}

Now, this code can load a resource into a picture box (and again, sorry for
the ugly code -- this is just a test project)
private void ResourcesListBo x_SelectedIndex Changed(object sender,
System.EventArg s e)
{
if(this.Resourc esListBox.Selec tedIndex >= 0 && this.TargetAsse mbly !=
null)
{
System.Drawing. Image image =
System.Drawing. Image.FromStrea m(this.TargetAs sembly.GetManif estResourceStre am((string)this .ResourcesListB ox.SelectedItem ));
this.pictureBox 1.Image = image;
}
}

Of course, this code assumes that the incoming resource is a bitmap. I
would really like a good way to verify the type of the incoming stream. It
seems like a possible attack vector if someone slips me a non-bitmap
resource.

Also, I think that loading the assembly in this way means that it cannot be
unloaded. I wonder if there is a better way load it?




"Dustin Aleksiuk" <du****@signale leven.com> wrote in message
news:31******** *****@individua l.net...
J.Marsch wrote:
Hello all:

I am trying to build a resource dll that will house icons, and I'm afraid
that I'm not having much luck reading it. Can you lend hand (or just
point me to a good primer).


I was just stuck on this too. I solved it by using a .resources file which
I linked in to the application the same way you mention in your
description (by setting that 'embeddable' flag on).

You can call it with the ResourceManager class. The string you pass into
the constructor is the name of the resources file without the suffix, and
you have to make sure that if you have an application namespace you
prepend it to the name.

So if my resources file was "myapplication. resources" and my application
namespace set in Visual Studio was "mynamespac e" I would pass in
"mynamespace.my application" into the constructor. You can then just call
.GetResourceSet on it.

I hope that helps a little.

Regards,
Dustin Aleksiuk

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
13110
by: Dennis C. Drumm | last post by:
I have ordered the book .NET Framework Solutions, In Search of the Lost Win32 API by John Paul Meuller which I think will help answer some of my questions I have regarding making custom MessageBoxes, but that isn't going to be here for a few days. So, I thought I would ask you guys how to put one of hte standard MessageBox icons, such as MessageBoxIcon.Exclamation, into my custom MessageBox. Thanks,
1
4552
by: Mats-Lennart Hansson | last post by:
Hi, I want to create a resource file with 2 icons and then use them in a c# project. First I create the resx file with reseditor.exe. After that I add the resx-file into my project and then I try to get the icon by using the code: Assembly a = Assembly.GetExecutingAssembly(); ResourceManager rm = new ResourceManager("Resource1", a); Icon i = (Icon) rm.GetObject("Running");
1
2529
by: Jake Wharton | last post by:
Right now i have embedded an icon file in my project with 6 different individual icons in it. I currently use Bitmap icon = new Bitmap(this.GetType(), "App.ico"); to load in the icon but it will only work on the first icon. I've stumbled upon a class called MultiIcon (http://www.codeproject.com/dotnet/MultiIcon.asp) which allows you to select an individual icon. My problem is how to load the resource into this class. I've added my...
3
1194
by: Miro | last post by:
Im really stuck on something. My goal is to get away from Image Lists and to use a Resource file instead. I followed an example and went to Properties of my project and clicked on the resource tab. Here I have found that I can add my images here. But how do I create a Resource file that has a differnet name...something like: MyIcons.rsex, so i can just put all my icons in 1 file.
7
2107
by: craig | last post by:
....quick question for anyone who might have some experience with .net resource files in VS 2003. I have an application that is not localized, but I would still like to be able to place all of the images that are used for icons across all of the forms in all of the assemblies in a single resource file so that swaping out an image in the resource file affects all localtions that it appears in the app. Is there a way to create a single...
1
1800
by: Nickneem | last post by:
The following is driving me mad: VS2005 Compact framework, project upgraded from 2003. I get a cast exeception error trying to populate an imagelist (code is by design) Me.imgListDiv.Images.Clear() Me.imgListDiv.Images.Add(CType(resources.GetObject("resource"), System.Drawing.Icon))
1
2686
by: John | last post by:
Is there such a thing as storing icons or bitmaps into a resource file, and adding more icons or bitmaps into the resource file at runtime? I want to store a bunch of icons into a resource file, and at runtime I want to be able to add more icons as I need to add. I don't want to use an imagelist because that will be a set number of icons in the program.
2
4697
by: bahnfire | last post by:
Hi, I am hoping that I finally have the right group :-) I am using Visual C++ Express 2008 and am trying to change the taskbar icon that I am using (notifyIcon1) as some state changes (button press, etc) in my form/app. I have read that I can use imageList1 and have the following: //
2
8759
by: Nathan Sokalski | last post by:
I am attempting to create icons for controls I have created using VB.NET by using the System.Drawing.ToolboxBitmap attribute. I have managed to do this in C# by specifying the path to the *.ico file, but I have been unable to get any of the overloads to work in VB.NET. I would like to store the *.ico files in a *.resx file so that users do not need anything other than the *.dll, but at the moment I am just trying to get any of the overloads...
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.