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

≡放荡痞女 提交于 2019-12-19 04:15:11

问题


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.

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:

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.

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.

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

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.


回答1:


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)



回答2:


I have tried this form, and it is functional

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



来源:https://stackoverflow.com/questions/8767755/how-do-you-load-an-image-from-resource-file-in-vb-2010-expresss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!