将所有(Properties.Resources)存储在Dictionary中

时间:2009-05-11 18:05:34

标签: c# visual-studio

基于ID我想自动将Image加载到我的GUI中。为此,我希望能够从Visual Studio 2008中的Resources.resx文件中获取所有图像(使用C#)。我知道如果我知道它们是什么,我一次可以得到一个:

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

然而,我正在寻找的是这些......

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

2 个答案:

答案 0 :(得分:11)

只需使用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
   } 
}

答案 1 :(得分:1)

好的,这似乎有效,但我欢迎其他答案。

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;
      }
   }
}