468,248 Members | 1,490 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,248 developers. It's quick & easy.

How do I access images in the resx file as if they are in an array?

DOSrelic
I have an assignment program that uses an array of images that are created from a supporting resource folder but I wish to incorporate them into the final exe file so how do I make an array from the Resources.resx file please?

Also, one of the images in the resx file is incorrect and should be removed. How can I remove that please? Every time I try anything with the Resources.resx file the Visual Studio 2008 IDE hates me!
Any knowledgeable advice would be greatly appreciated, thanks.

Thanks & Best Regards
[DOSrelic]
`._.._.
Nov 2 '09 #1

✓ answered by tlhintoq

That is just way messy and old-school.

The ResourceManager already knows all the resources let's just ask it.
Resources are kept on a per-culture basis. This is so you can make your program culture-specific. You can have resources if you deploy in English, a different set for Spanish and so on. We are simply going to get all the resources for English in this case.

Expand|Select|Wrap|Line Numbers
  1. System.Resources.ResourceSet temp = Properties.Resources.ResourceManager.GetResourceSet(new CultureInfo("en-US"), false,false);
  2. if (temp == null)
  3. {
  4.     // No resources specific to English
  5.     // Look for other resourses in the language of your application
  6. }
  7. foreach (System.Collections.DictionaryEntry set in temp)
  8. {
  9.     Console.WriteLine(string.Format("{0}: {1}", set.Key, set.Value));
  10. }
Try running this. It will show you the full list of resources and their types. From there you can sort a list, throw out ones that don't start with the word "card" for example etc.

13 4224
Plater
7,872 Expert 4TB
Are you using the resource management stuff that VS does or are you trying to directly edit the file?
Nov 2 '09 #2
Hi Plater, The resource management stuff that VS does but there does not seem to be the ability to remove an image. I currently load them from files into an array as I don't know how to use the VS way so the program requires it's own resource sub-folder, thanks.
Nov 2 '09 #3
Hi there I have just started with Csharp and wrote a messy but usable memory card game using a folder with all the images of the cards but I really want to be able to store them in the executable via the project resource file. I can load the resource file but do not know how to retrieve them to assign them to the newly created card class objects.
Instead of the line:
static Bitmap _back = new Bitmap(AmemoryGame .cardBackFileNames[0]);
what should I use please? I sort of figured that I would use ResXResourceReader resxReader = new ResXResourceReader(...)but then realised I didn't even know the name of the file!
Can anyone give me some guidance on how to access my resx resource file images please? Any knowledgeable help would be greatly cheers.
Dec 27 '09 #4
tlhintoq
3,525 Expert 2GB
You can reach your resource images by reference.

Expand|Select|Wrap|Line Numbers
  1. MyImage = MyProgramNamespace.Properties.Resources.G5_iMac;
Dec 27 '09 #5
@tlhintoq
Thank you tlhintoq, I can now access the resource images individually but is there a way to sort them and put them into an array? I currently have an array of file names so I can assign a unique image to every new card object in a loop:
Expand|Select|Wrap|Line Numbers
  1. _cards[index].FrontBm = new Bitmap(AmemoryGameGlobals.cardFrontFileNames[index++]);
Dec 27 '09 #6
tlhintoq
3,525 Expert 2GB
I see no reason why you couldn't get a list of the resources, sort it, then assign within a loop.
Dec 27 '09 #7
Neither do I tlhintoq but I'm afraid I wouldn't know how to go about doing that! Can you offer any sage advice on that score please? I mean I have a list of resource names but how do I transfer them to an array automatically? Properties.Resources.ResourceManager.....? Are they not already held in an array of some type and if so can I not set an array to that?
Dec 28 '09 #8
alexis4
113 100+
I could think that as a "stack pointer" type of solution. Have a counter variable starting from 0, increasing every time a picture gets in resource file and decreasing every time a picture gets out. In this way you can retrieve any element of the array at any time. Build that in an array structure or class and have all the information about your image inside this class at the specific counter's position (name, user properties...).
If this is not enough (it depends on the application) you can "stick" this counter value at the end of your picture names (kept inside the class): MyPicture00, ThisPicture07, AnyPicture38. So when i.e. an event comes, convert sender's name to char array and keep the last 2 characters (0<=picture_number<100 case). Convert it to int and you will have the counter's value. With this value you know the exact element of the array that triggered the event or you can use this value in a switch-case or do with it whatever you need.

It's a bit low level, but it works.
Dec 28 '09 #9
Hi alexis4, interesting proposal but the resource images arealready preloaded and I just need to sequentially assign them. Does anyone know how to do that please?
Dec 28 '09 #10
tlhintoq
3,525 Expert 2GB
That is just way messy and old-school.

The ResourceManager already knows all the resources let's just ask it.
Resources are kept on a per-culture basis. This is so you can make your program culture-specific. You can have resources if you deploy in English, a different set for Spanish and so on. We are simply going to get all the resources for English in this case.

Expand|Select|Wrap|Line Numbers
  1. System.Resources.ResourceSet temp = Properties.Resources.ResourceManager.GetResourceSet(new CultureInfo("en-US"), false,false);
  2. if (temp == null)
  3. {
  4.     // No resources specific to English
  5.     // Look for other resourses in the language of your application
  6. }
  7. foreach (System.Collections.DictionaryEntry set in temp)
  8. {
  9.     Console.WriteLine(string.Format("{0}: {1}", set.Key, set.Value));
  10. }
Try running this. It will show you the full list of resources and their types. From there you can sort a list, throw out ones that don't start with the word "card" for example etc.
Dec 28 '09 #11
Thanks again tlhintoq, another great step towards my desired goal but I was wondering if the GetEnumerator property could be used in my loop and if so how would I do that please? Unfortunately I am unaware of how to use it although the IDE advises me that is can iterate through the resource set?
Dec 28 '09 #12
tlhintoq
3,525 Expert 2GB
At this point I'm going to recommend experimentation and the MSDN.
Dec 28 '09 #13
Okay tlhintoq, thanks for your help. :)
Dec 29 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Jin P via .NET 247 | last post: by
reply views Thread by David Krussow | last post: by
reply views Thread by prabhupr | last post: by
1 post views Thread by CKKwan | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.