C# 获取要存储在字典中的所有(Properties.Resources)
基于一个ID,我想自动将一个图像加载到我的GUI中。为此,我希望能够使用C从Visual Studio 2008中的Resources.resx文件中获取所有图像。我知道如果我知道它们是什么,我可以一次获取一个:C# 获取要存储在字典中的所有(Properties.Resources),c#,visual-studio,C#,Visual Studio,基于一个ID,我想自动将一个图像加载到我的GUI中。为此,我希望能够使用C从Visual Studio 2008中的Resources.resx文件中获取所有图像。我知道如果我知道它们是什么,我可以一次获取一个: Image myPicture = Properties.Resources.[name of file]; 然而,我所寻找的是沿着这些路线 foreach(Bitmap myPicture in Properties.Resources) {Do something...} 好的
Image myPicture = Properties.Resources.[name of file];
然而,我所寻找的是沿着这些路线
foreach(Bitmap myPicture in Properties.Resources) {Do something...}
好的,这似乎是工作,但我会欢迎其他答案
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;
}
}
}
只需使用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
}
}
我喜欢这样,尤其是当resource.resx文件中有其他数据类型时。我还没有研究Linq,所以我假设这将生成执行此任务的代码。从上面的答案中,你知道速度有什么不同吗?我猜这将是微不足道的,但总是最好问。谢谢你的回答!没有codegen,这是代码。至于速度,应该在同一个范围内。