好的,所以我创建了一个dictionary
包含字符串和相应bitmap
图像的对象。我已将所有相关图像正确添加到项目资源中,并尝试了几种不同的方法将它们添加到dictionary
运行时中,以便根据输入的字母更轻松地访问图像。这是我用于创建dictionary
和尝试从我的资源中填充它的代码。
Dictionary<String,Image> namesAndImages = new Dictionary<String, Image>();
var resources = Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
foreach (System.Collections.DictionaryEntry myResource in resources)
{
if (myResource.Value is Bitmap) //is this resource is associated with an image
{
String resName = myResource.Key.ToString(); //get resource's name
Image resImage = myResource.Value as Image; //get the Image itself
namesAndImages.Add(resName, resImage);
}
}
无论出于何种原因,它似乎都没有将任何图像添加到字典中。我已经测试了几个不同的键值对,但根本没有任何内容。大家有什么想法吗?
您可以使用与给定资源文件相关联<Resource file name>.ResourceManager
的获取ResourceManager
。可以像这样访问图像:
Dictionary<string, Image> dict = new Dictionary<string, Image>();
var resourceManager = Resource1.ResourceManager;
var resources = resourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
foreach (DictionaryEntry item in resources)
{
if (item.Value is Bitmap)
{
dict.Add(item.Key as string, item.Value as Image);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句