Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the names of all resources in a resource file

Within a Visual Basic Project I have added a resource file (resx) that contains a bunch of images.

Now I want to query the names of the images. If I open the resx file in the designer view in the Visual Studio IDE and select an image, the property grid shows me a name property (defaults to "filename without extension but can be changed).

The background is that I have a imagelist that is created at runtime and populated with the images from the resource file. To be able to access these images by the key, I have to set it.

My code looks like this (everything hard coded):

Dim imagelist as new Imagelist
imageList.Images.Add("A", My.Resources.MyImages.A)
imageList.Images.Add("B", My.Resources.MyImages.B)
imageList.Images.Add("C", My.Resources.MyImages.C)
imageList.Images.Add("D", My.Resources.MyImages.D)
imageList.Images.Add("E", My.Resources.MyImages.E)
....
imageList.Images.Add("XYZ", My.Resources.MyImages.XYZ)

And I want to achive this:

Dim imagelist as new ImageList

For Each img in GetMeAllImagesWithNameFromMyResourceFile
    imageList.Images.Add(img.Name, img.ImageFile)
Next

where Name is a string and ImageFile a System.Drawing.Bitmap

like image 242
Jürgen Steinblock Avatar asked Jun 16 '09 09:06

Jürgen Steinblock


2 Answers

See if this piece of code helps.

    Dim runTimeResourceSet As Object
    Dim dictEntry As DictionaryEntry

    runTimeResourceSet = My.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, False, True)
    For Each dictEntry In runTimeResourceSet
        If (dictEntry.Value.GetType() Is GetType(Icon)) Then
            Console.WriteLine(dictEntry.Key)
        End If
    Next

I have used Icon as an example, which you will have to change if you are using Bitmap.

EDIT: You will have to use reference of dictEntry.Value & see how it can be used for adding it to imagelist.

like image 76
shahkalpesh Avatar answered Nov 15 '22 06:11

shahkalpesh


The following is written in C#, you should be able to translate this to VB easily.

Assembly executingAssembly = GetExecutingAssembly();

foreach (string resourceName in executingAssembly.GetManifestResourceNames())
{
    Console.WriteLine( resourceName );
}

Now that you have all the resource names, you can iterate over the list and do something like:

foreach(string s in executingAssembly.GetManifestResourceNames())
{
    if (s.EndsWith(".bmp"))
    {
        imgStream = a.GetManifestResourceStream(s);
        if (imgStream != null)
        {                    
            bmp = Bitmap.FromStream(imgStream) as Bitmap;
            imgStream.Close();
        }   
    }
}

I have not tried this, but it should work.

like image 25
Thorsten Dittmar Avatar answered Nov 15 '22 07:11

Thorsten Dittmar