Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all (Properties.Resources) to be stored in a Dictionary

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...}
like image 341
Billy Avatar asked May 11 '09 18:05

Billy


2 Answers

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
   } 
}
like image 74
Shay Erlichmen Avatar answered Nov 04 '22 15:11

Shay Erlichmen


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;
      }
   }
}
like image 24
Billy Avatar answered Nov 04 '22 15:11

Billy