Visual studio image loading in for cycle from resources

Deadly 提交于 2019-12-11 06:40:56

问题


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

回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/20353030/visual-studio-image-loading-in-for-cycle-from-resources

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