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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DOSrelic
    New Member
    • Sep 2006
    • 13

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

    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]
    ¨`•._.®._.•´¨
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Are you using the resource management stuff that VS does or are you trying to directly edit the file?

    Comment

    • DOSrelic
      New Member
      • Sep 2006
      • 13

      #3
      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.

      Comment

      • DOSrelic
        New Member
        • Sep 2006
        • 13

        #4
        How do I access images in the resource file as tho' they are in an array?

        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(AmemoryG ame .cardBackFileNa mes[0]);
        what should I use please? I sort of figured that I would use ResXResourceRea der resxReader = new ResXResourceRea der(...)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.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          You can reach your resource images by reference.

          Code:
          MyImage = MyProgramNamespace.Properties.Resources.G5_iMac;

          Comment

          • DOSrelic
            New Member
            • Sep 2006
            • 13

            #6
            Originally posted by tlhintoq
            You can reach your resource images by reference.

            Code:
            MyImage = MyProgramNamespace.Properties.Resources.G5_iMac;
            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:
            Code:
            _cards[index].FrontBm = new Bitmap(AmemoryGameGlobals.cardFrontFileNames[index++]);

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              I see no reason why you couldn't get a list of the resources, sort it, then assign within a loop.

              Comment

              • DOSrelic
                New Member
                • Sep 2006
                • 13

                #8
                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.Reso urces.ResourceM anager.....? Are they not already held in an array of some type and if so can I not set an array to that?

                Comment

                • alexis4
                  New Member
                  • Dec 2009
                  • 113

                  #9
                  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_num ber<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.

                  Comment

                  • DOSrelic
                    New Member
                    • Sep 2006
                    • 13

                    #10
                    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?

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      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.

                      Code:
                      System.Resources.ResourceSet temp = Properties.Resources.ResourceManager.GetResourceSet(new CultureInfo("en-US"), false,false);
                      if (temp == null)
                      {
                          // No resources specific to English
                          // Look for other resourses in the language of your application
                      }
                      foreach (System.Collections.DictionaryEntry set in temp)
                      {
                          Console.WriteLine(string.Format("{0}: {1}", set.Key, set.Value));
                      }
                      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.

                      Comment

                      • DOSrelic
                        New Member
                        • Sep 2006
                        • 13

                        #12
                        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?

                        Comment

                        • tlhintoq
                          Recognized Expert Specialist
                          • Mar 2008
                          • 3532

                          #13
                          At this point I'm going to recommend experimentation and the MSDN.

                          Comment

                          • DOSrelic
                            New Member
                            • Sep 2006
                            • 13

                            #14
                            Okay tlhintoq, thanks for your help. :)

                            Comment

                            Working...