VB.Net:从 My.Resources 动态选择图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1190729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:04:46  来源:igfitidea点击:

VB.Net: Dynamically Select Image from My.Resources

.netvb.netimagecontrolsmy.resources

提问by Jeff

I have a group of images in my My.Resources. I want to select select images to display dynamically at run time. How do I do this?

我的 My.Resources 中有一组图像。我想选择要在运行时动态显示的选择图像。我该怎么做呢?

'Static (Compile time) Assignment
UltraPictureBox1.Image = my.Resources.zoo_picture_1

'Dynamic (Runtime) Assignment
UltraPictureBox1.Image = ???

回答by Jeff

Found the solution:

找到了解决办法:

UltraPictureBox1.Image = _
    My.Resources.ResourceManager.GetObject(object_name_as_string)

回答by Andrea Antonangeli

This works for me at runtime too:

这在运行时也适用于我:

UltraPictureBox1.Image = My.Resources.MyPicture

No strings involved and if I change the name it is automatically updated by refactoring.

不涉及字符串,如果我更改名称,它会通过重构自动更新。

回答by Samuel Couture Brochu

Make sure you don't include extension of the resource, nor path to it. It's only the resource file name.

确保您不包含资源的扩展名,也不包含它的路径。这只是资源文件名。

PictureBoxName.Image = My.Resources.ResourceManager.GetObject("object_name") 

回答by Sravan.Sura

Dim resources As Object = My.Resources.ResourceManager
PictureBoxName.Image = resources.GetObject("Company_Logo")

回答by PeterN

Sometimes you must change the name (or check to get it automatically from compiler).

有时您必须更改名称(或检查以从编译器自动获取它)。

Example:

例子:

Filename = amp2-rot.png

文件名 = amp2-rot.png

It is not working as:

它不能作为:

PictureBoxName.Image = resources.GetObject("amp2-rot.png")

It works, just as amp2_rotfor me:

它有效,就像amp2_rot对我一样:

 PictureBox_L1.Image = My.Resources.Resource.amp2_rot