[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to access System.Windows.Forms resources?

Posted on 2009-06-29
13
Medium Priority
?
494 Views
Last Modified: 2013-12-17
I would like to access the resources in System.Windows.Forms, specifically icons and assign them to misc buttons.

I know I could rip them and add them to the Resource file of the Project, however that would be a lot of work considering the number of icons I need to use.

Please provide small code sample in C#, thanks in advance!
reflector.png
0
Comment
Question by:LittleJohn101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
13 Comments
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24737759
You can get a list of all resources in the Forms assembly by using the following code snippet. You can then use

Stream iconStream = a.GetManifestResourceStream(stringOfResource);

to get the stream of a resource.


// list all resources for the Window.Forms resource, 
// assuming System.Window.Forms is loaded and in focus
// (otherwise use a fully qualified name)
 
Assembly asm = Assembly.GetAssembly(typeof(Form));
string[] resources = asm.GetManifestResourceNames();
foreach (string s in resources)
    Debug.WriteLine(s);

Open in new window

0
 

Author Comment

by:LittleJohn101
ID: 24737897
This looks good, is there anyway to access these from the VS 2005 Form Designer?
0
 
LVL 5

Expert Comment

by:iUsername
ID: 24737917
Do you mean:
"global::<projectname>.Properties.Resources" ???
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:LittleJohn101
ID: 24737929
Exactly, I have tried

global::System.Windows.Forms.Resources
global::System.Windows.Forms.Properties

with no success...
0
 
LVL 39

Expert Comment

by:abel
ID: 24737952
> This looks good, is there anyway to access these from the VS 2005 Form Designer?
You cannot access these elements directly from the form designer because the Visual Studio has never seemed it necessary to make it possible to browse the resources inside a DLL directly from the Form designer (i.e., when you select Image of a Picturebox). It is however possible to load the DLL directly in VS to browse the resources, but then you'd have to save them to another location.

Doing that in one time in a loop as I showed above (you can simply copy the Stream to disk) is then much easier.

If you want to access them by code, you can do so too, using just the same technique.

-- Abel --
0
 
LVL 39

Expert Comment

by:abel
ID: 24737969
> global::System.Windows.Forms.Resources
That won't work, because that should mean that the Resources property should be implemented and made public. The Resources property the way you know it is added by Visual Studio as a convenience but does not have to be implemented.
0
 

Author Closing Comment

by:LittleJohn101
ID: 31598003
Thanks for your abel!
0
 
LVL 39

Expert Comment

by:abel
ID: 24738031
Just tried it as a little experiment, to be sure what I was telling was correct and the screenshot shows a few of the output bmp files that I have from the Forms.dll. Use the following code to "dump" all the ico, bmp and other resources from any dll:

Assembly asm = Assembly.GetAssembly(typeof(Form));
string[] resources = asm.GetManifestResourceNames();
foreach (string s in resources)
{
    using (Stream strIn = asm.GetManifestResourceStream(s))
    using(Stream strOut = File.Open(@"d:\temp\data\" + s, FileMode.OpenOrCreate))
    {
        BufferedStream outStream = new BufferedStream(strOut);
        BufferedStream inStream = new BufferedStream(strIn);
        byte[] buf = new byte[4096];
        int bytesRead = 0;
        while ((bytesRead = inStream.Read(buf, 0, 4096)) > 0)
            outStream.Write(buf, 0, bytesRead);
 
        outStream.Flush();
    }
}

Open in new window

ScreenShot409.png
0
 

Author Comment

by:LittleJohn101
ID: 24738040
Nice!
0
 
LVL 39

Expert Comment

by:abel
ID: 24738055
You're welcome (just saw you already graded, but figured you'd like to know how to access / save the resource) ;-)
0
 
LVL 5

Expert Comment

by:iUsername
ID: 24738286
p.s.

it is not "global::System.Windows.Forms.Resources".
You shouldn't write "System" but your project's name....
As far as I know (and I'm using this code), it should work.

Anyway, glad it's working that other way too! :-) (and I learned something new...)
0
 
LVL 39

Expert Comment

by:abel
ID: 24738318
> You shouldn't write "System" but your project's name....
> As far as I know (and I'm using this code), it should work.

You may want to check the OP's question again: (s)he doesn't want his/her self created resources, but the resources from another DLL, which is in this case the loaded DLL System.Windows.Forms.DLL. You cannot access that by using your own classname, because that would refer to your own class.
0
 
LVL 5

Expert Comment

by:iUsername
ID: 24740460
Right.
Sorry, and thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question