views:

281

answers:

2

Based on an ID I would like to automatically load an Image into my GUI. To do this I would like to be able to get all images out of the Resources.resx file in Visual Studio 2008 (using C#). I know I'm able to get one at a time if I know what they are:

Image myPicture = Properties.Resources.[name of file];

However what I'm looking for is along these lines...

foreach(Bitmap myPicture in Properties.Resources) {Do something...}
A: 

Ok this seems to be working, however I would welcome other answers.

ResourceManager rm = Properties.Resources.ResourceManager;

ResourceSet rs = rm.GetResourceSet(new CultureInfo("en-US"), true, true);

if (rs != null)
{
   IDictionaryEnumerator de = rs.GetEnumerator();
   while (de.MoveNext() == true)
   {
      if (de.Entry.Value is Image)
      {
         Bitmap bitMap = de.Entry.Value as Bitmap;
      }
   }
}
Billy
A: 

Just use Linq (tm)

ResourceManager rm = Properties.Resources.ResourceManager;

ResourceSet rs = rm.GetResourceSet(new CultureInfo("en-US"), true, true);

if (rs != null)
{
   var images = 
     from entry in rs.Cast<DictionaryEntry>() 
     where entry.Value is Image 
     select entry.Value;

   foreach (Image img in images)
   {
     // do your stuff
   } 
}
Shay Erlichmen
I like this, especially if there are other data types in the resource.resx file. I haven't studied Linq yet, so I'm assuming that this will generate code to do this task. Any idea what the difference in speed will be from the answer above? I'm guessing it will be negligible, but always best to ask. Thank you for the Answer!
Billy
there is no codegen, this is the code. As for speed, it should be in the same ballpark.
Shay Erlichmen
Thank you for your help Shay!
Billy