Link to home
Create AccountLog in
Avatar of JedNebula
JedNebulaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Iterate through My.Resources

I have done a few Google searches and not found anything so I'm thinking it can't be done, but I'm wondering if there is a way to loop (or iterate) through the My.Resources so I can do something with each icon/image for example.

Something like this:

For each ico as Icon in My.Resources
    .....
Next ico
Avatar of kojoru
kojoru
Flag of Russian Federation image

If you're OK in C#, try reading this article that covers the exact question you ask and many more things regarding resources.
Here's a little simplified VB variant of the code provided in the article.

You should add multiline TextBox txtInfo for this to work.

Public Class Form1
    Dim pics As System.Collections.ArrayList = New ArrayList
 
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim imgStream As IO.Stream
        Dim bmp As Bitmap
 
        Dim a As Reflection.Assembly
        a = Reflection.Assembly.GetExecutingAssembly()
        Dim ResNames As String()
        ResNames = a.GetManifestResourceNames()
 
        txtInfo.Clear()
        txtInfo.Text += String.Format("Found {0} resources\r\n", ResNames.Length)
        txtInfo.Text += "----------\r\n"
 
        For Each s As String In ResNames
 
 
 
            txtInfo.Text += s + "\r\n"
            If s.EndsWith(".bmp") Then
 
 
 
                ' attach to stream to the resource in the manifest
                imgStream = a.GetManifestResourceStream(s)
 
 
                'create a new bitmap from this stream and 
                'add it to the arraylist
                bmp = Bitmap.FromStream(imgStream)
 
 
                pics.Add(bmp)
 
                imgStream.Close()
 
            End If
        Next
        txtInfo.Text += "----------\r\n"
        txtInfo.Text += String.Format("Found {0} Bitmaps\r\n", pics.Count)
 
    End Sub
End Class

Open in new window

Avatar of JedNebula

ASKER

Thanks guys.

I'm finding that it's not picking up the images in my.Resources. The images are Embedded in .resx and they are ico and .bmp formats. (see image)

Here is the result from the textbox:

Found 22 resources\r\n----------\r\nEcho.Resources.resources\r\nEcho.frmEditServer.resources\r\nEcho.frmChangeLog.resources\r\nEcho.frmTaskProgress.resources\r\nEcho.frmOptions.resources\r\nEcho.frmMDIStatus.resources\r\nEcho.frmHASPError.resources\r\nEcho.frmConnect.resources\r\nEcho.frmMain.resources\r\nEcho.frmTask.resources\r\nEcho.exe.licenses\r\nEcho.frmTray.resources\r\nEcho.frmLogon.resources\r\nEcho.frmExceptionLog.resources\r\nEcho.ServerDetailsControl.resources\r\nEcho.TasksActionControl.resources\r\nEcho.frmExceptionHandler.resources\r\nEcho.frmSecurity.resources\r\nEcho.frmExpBar.resources\r\nEcho.frmEditServers.resources\r\nEcho.frmTest.resources\r\nEcho.frmSplash.resources\r\n----------\r\nFound 0 Bitmaps\r\n
experts-exchange.jpg
ASKER CERTIFIED SOLUTION
Avatar of MijaeDjinn
MijaeDjinn
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer