如何将资源文件夹中的图像添加到字典

麦克人

好的,所以我创建了一个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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将下载的图像添加到资源文件夹

如何将图像文件夹添加到在 React 中呈现数组的组件

如何将多个源文件夹添加到JAR?

如何将gradle生成的源文件夹添加到Eclipse项目?

如何将图像添加到可绘制资源文件

如何将另一个测试源文件夹添加到Maven并将其编译到单独的文件夹中?

将外部资源文件夹添加到Spring Boot

如何将文件夹添加到.gitignore

如何将修剪的圆形按钮图像添加到android drawable文件夹?

如何将图像从不同文件夹添加到 HTML

C# - 如何将文件夹中的图像添加到数组并在列表视图中显示它们?

如何将GIF图像添加到Assets文件夹并以编程方式将其加载到UIImageView中

linux如何将文件添加到zip文件中的特定文件夹

如何将框架添加到 Xcode Playground 的“Sources”文件夹中的文件中?

将SVG图像添加到资源文件

使用Maven将资源文件夹下的文件添加到zip中

如何将文件和文件夹添加到GitHub存储库中?

如何将Java类添加到jar文件中的文件夹

如何将文件夹文件的修改日期添加到GridView中?

如何将图像保存到资源文件夹java swing

如何将 Localhost\Users + READ 权限添加到 Powershell 中的文件夹

如何将公用文件夹添加到react-boilerplate webpack配置中?

在generate-sources将源文件夹添加到我的Eclipse Project中

如何将FileInputStream获取到资源文件夹中的文件

如何将资源文件夹从jar复制到程序文件中

如何将资源添加到jar文件中

如何将资源文件添加到我的phonegap项目

如何将背景图像设置为源文件夹中的图像?

如何将可绘制文件夹中的图像添加到Android Studio 1.2中的ImageView?