vb.net 你如何在 vb 2010 express 中从资源文件加载图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8767755/
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-09 15:51:33  来源:igfitidea点击:

How do you load an image from resource file in vb 2010 expresss?

vb.netvisual-studio-2010resourcesmy.resources

提问by CustomZ02

First of all Im new to vb 2010 and so far have enjoyed what I have been able to do with it. That being said I have run into an issue with my current project.

首先,我是 vb 2010 的新手,到目前为止我很享受我能用它做的事情。话虽如此,我在当前项目中遇到了问题。

Basically I have created a timer and all works well on that part. My issue lies in that my timer loads a .png for each minute/second and I was linking the images like so:

基本上我已经创建了一个计时器,并且在该部分一切正常。我的问题在于我的计时器每分钟/秒加载一个 .png 并且我像这样链接图像:

Picturebox1.Image = Image.Fromfile("C:\timer\images\" & minutes.text & ".png")
Picturebox2.Image = Image.Fromfile("C:\timer\images\" & seconds.text & ".png")

So running this on another pc rendered that bit of code useless as that computer did not have those files locally and the program would end in an error as it could not find the .png files.

因此,在另一台 PC 上运行它会使那段代码变得无用,因为该计算机在本地没有这些文件,并且程序将以错误结尾,因为它找不到 .png 文件。

I did a bit of searching online and found a few sites and video tutorials how to read from the resource file. But in doing so I have been unable to make it function properly.

我在网上做了一些搜索,找到了一些网站和视频教程,介绍了如何从资源文件中读取。但是这样做我一直无法使其正常运行。

So this is what I found here:

所以这就是我在这里找到的:

Picturebox1.image = My.Resources.minutes.text
Picturebox2.image = My.Resources.seconds.text

I know this bit of code is wrong as I'm now getting 2 errors in vb 2010. The only way I have managed to make this work is to specify the file name. But what I'm wanting to do is use whats in "minutes.text" and "seconds.text" to specify the file name.

我知道这段代码是错误的,因为我现在在 vb 2010 中遇到了 2 个错误。我设法完成这项工作的唯一方法是指定文件名。但是我想要做的是使用“minutes.text”和“seconds.text”中的内容来指定文件名。

Is there a way around this? or do I have to use a bunch of if statements to do this?

有没有解决的办法?还是我必须使用一堆 if 语句来做到这一点?

example:

例子:

If minutes.text = 1 Then
picturebox1 = My.Resource._1
End If
If seconds.text = 12 Then
Picturebox2 = My.Resource._12
End If

I would hate having to do a bunch of if statements if there is a simple fix. So I've come here for help.

如果有一个简单的修复,我会讨厌必须做一堆 if 语句。所以我来这里寻求帮助。

回答by John Woo

i think you are looking for this:

我想你正在寻找这个:

Dim currentMin as string = "_" & minutes.text ' it would look something like this: _1
picturebox1.Image = CType(My.Resources.ResourceManager.GetObject(currentMin), Image)

Dim currentSec as string = "_" & seconds.text
picturebox2.Image = CType(My.Resources.ResourceManager.GetObject(currentSec), Image)

回答by Elias

I have tried this form, and it is functional

我试过这种形式,它是功能性的

Picturebox1.Image = Image.FromHbitmap(My.Resources.imagename.GetHbitmap())

Picturebox1.Image = Image.FromHbitmap(My.Resources.imagename.GetHbitmap())