从资源中加载Visual Studio图像以进行循环 [英] Visual studio image loading in for cycle from resources

查看:160
本文介绍了从资源中加载Visual Studio图像以进行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的图像资源中,我的图片具有以下名称:c1.png,c2.png,c3.png,... 而引用第一张图片可使用My.Resources.c1

In my image resources I have pictures with following names: c1.png, c2.png, c3.png, ... Whereas referring to the first image works with My.Resources.c1

我想像这样在循环中显示它们

I would like to display them in the cycle like this

        For i = 1 To numberOfPictures
            Dim tmpPicture As New PictureBox

            tmpPicture.Image = cstr(My.Resources.c) & cstr(i) & ".png"
        next

现在这当然不起作用,因为无法将字符串转换为System.Drawing.Image. 任何想法如何解决这个问题?我知道在VB中这样做很容易:

Now this of course doesn't work, because string cannot be converted to System.Drawing.Image. Any ideas how to solve this? I know it's really easy in VB where I did it like this:

        For i = 1 To numberOfPictures
            imgName.Picture = loadPicture(ThisWorkbook.Path & "\picturesfolder\" & CStr(i) & ".png")
        next i

推荐答案

是的,您可以这样做:

tmpPicture.Image = DirectCast(My.Resources.ResourceManager.GetObject("c" & CStr(i) & ".png"), Bitmap)

这是一个提示.有时,查看设计器代码非常有用.如果右键单击My.Resources.c1并选择转到定义选项,则在访问该属性时,您将看到实际执行的代码.很明显,c1属性不是作为.NET框架的一部分内置的.它是Designer文件中的自动生成的属性.资源设计器屏幕会自动为您生成该代码.

Here's a hint. Sometimes it's very useful to look at the designer code. If you right-click on My.Resources.c1 and choose the Go to Definition option, you'll see the code that is actually executed when you access that property. The c1 property is, obviously, not built in as part of the .NET framework. It's an auto-generated property in a Designer file. The Resources designer screen automatically generates that code for you.

这篇关于从资源中加载Visual Studio图像以进行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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