Visual studio image loading in for cycle from reso

2019-08-11 13:05发布

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

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

4条回答
迷人小祖宗
2楼-- · 2019-08-11 13:30

Thanks to Steven's terrific answer I was able to figure out the C# usage

tmpPicture.Image = (System.Drawing.Bitmap)Resources.ResourceManager.GetObject("c" & i.toString());

although my image was an icon and actual code that worked was

_image = (System.Drawing.Icon)Resources.ResourceManager.GetObject(Type + imageNo.toString());

Please up vote Stevens answer(they wont let me yet). That kind of answer is helpful to those who desperately need a quick answer AND for those who want to learn coding.

查看更多
小情绪 Triste *
3楼-- · 2019-08-11 13:31

You have to put the images you'd like to cycle into a collection, then you can do a for loop on the collection.

It will look something like this:

Dim bmpCol As New Collection(Of Bitmap)
bmpCol.Add(My.Resources.c1)
bmpCol.Add(My.Resources.c2)
'etc
Dim doCycle As Boolean = True
While doCycle
    For Each bmp As Bitmap In bmpCol
        tmpPicture.Image = bmp
    Next
End While

You can define the collection globally and add the images to it in the constructor.

You could also have a look on the My.Resources.ResourceManager. Currently i'm not sure if it provides the desired functionality...

Edit: Well, see Steven Doggart post for that answer.

查看更多
倾城 Initia
4楼-- · 2019-08-11 13:34

Try this:

    For i = 1 To numberOfPictures
        Dim tmpPicture As New PictureBox
        Dim objImage = DirectCast(My.Resources.ResourceManager.GetObject("c" & i), Bitmap)
        tmpPicture.Image = objImage
    Next

It's pretty self-explanatory, but basically, you're just using the GetObject method of the ResourceManager singleton instance to return the resource you specify dynamically at runtime rather than referencing them how you would normally by their static references (e.g. My.Resources.c1) at design time.

查看更多
该账号已被封号
5楼-- · 2019-08-11 13:45

Yes, you can do that like this:

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

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.

查看更多
登录 后发表回答