从Resources/ResourceManager加载图像

本文关键字:加载 图像 ResourceManager Resources | 更新日期: 2023-09-27 18:19:29

我有一个picturebox,我想在运行时根据用户在文本框中键入的内容将图像加载到其中。

我在我的项目中创建了一个名为Formations.resx的文件,并将我的图像加载到其中。我尝试了以下两种方法,但都没有成功,我做错了什么?

pictureBoxFormation.Image = Properties.Resources.ResourceManager.GetObject("random_" + firstPoint) as Image;

这段代码执行得很好,但我的picturebox的.Image属性没有被它更新,我猜这段代码在我的文件中没有具体查找,它在哪里查找?

ResourceManager rm = new ResourceManager("Formations", Assembly.GetExecutingAssembly());
pictureBoxFormation.Image = rm.GetObject("random_" + firstPoint) as Image;

这会抛出一个错误,如下所示:

An unhandled exception of type 'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll

Additional information: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Formations.resources" was correctly embedded or linked into assembly "WindowsFormsApplication1" at compile time, or that all the satellite assemblies required are loadable and fully signed.

从Resources/ResourceManager加载图像

最终按照Hans Passant的建议,将资源移动到Properties/resources,这在原始帖子的评论中有所描述。