你如何在 vb 2010 express 中从资源文件加载图像? [英] How do you load an image from resource file in vb 2010 expresss?

查看:50
本文介绍了你如何在 vb 2010 express 中从资源文件加载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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")

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

我知道这段代码是错误的,因为我现在在 vb 2010 中遇到了 2 个错误.我设法完成这项工作的唯一方法是指定文件名.但是我想要做的是使用minutes.text"和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.

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

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

示例:

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

如果有一个简单的修复,我会讨厌必须做一堆 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.

推荐答案

我认为您正在寻找这个:

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)

这篇关于你如何在 vb 2010 express 中从资源文件加载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆