0

I was able to use code in this link to view a list of items in a .resx file

using System.Collections;
using System.Globalization;
using System.Resources;

...
string resKey;
ResourceSet resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
{
    resKey = entry.Key.ToString();
    ListBox.Items.Add(resKey);
}

What I want to do now, is create an accessable list or array. How do I go about doing this? To clarify, I wan't to create an array of Image containers, and use a loop to load the images in the .resx file. Thanks

Community
  • 1
  • 1
Laserbeak43
  • 595
  • 2
  • 8
  • 21
  • List of arrayS did you mean? What do you mean exactly? Arrays of what type do you want to get and from what? – EngineerSpock Jan 23 '14 at 03:30
  • I am not sure why you want to use .resx file in a WPF project unless you are migrating a legacy project. Why not use 'ResourceDictionary' in WPF? – Praggie Jan 23 '14 at 03:31
  • @Praggie I just used an example on This forum and MSDN. Both used ResourceDictionary. It worked, I don't know of any other method. – Laserbeak43 Jan 23 '14 at 03:37

1 Answers1

1

I`m not sure that I get you right but probably this is what you want:

var resources = new List<string>();
foreach (DictionaryEntry entry in resourceSet)
{
    resources.Add(entry.Key.ToString());
}

UPDATE

Ok, then here is another solution. You can iterate through values of your resourceSet and if any value is a Bitmap - convert it into BitmapImage and add to your list. Like this:

var images = resourceSet.Cast<DictionaryEntry>()
            .Where(x => x.Value is Bitmap)
            .Select(x => Convert(x.Value as Bitmap))
            .ToList();

public BitmapImage Convert(Bitmap value)
{
    var ms = new MemoryStream();
    value.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    var image = new BitmapImage();
    image.BeginInit();
    ms.Seek(0, SeekOrigin.Begin);
    image.StreamSource = ms;
    image.EndInit();

    return image;
}
JleruOHeP
  • 10,106
  • 3
  • 45
  • 71
  • I don't see a `Select()` method in _System.Resources.ResourceSet_ – Laserbeak43 Jan 23 '14 at 05:09
  • Thank you, it seems this is not even what i want to do. It's actually just a cleaner way to get the info from my resource. I want to try to make some sort of elegant of type `System.Drawing.Bitmap` to load into another list of type `System.Windows.Media.ImageSource`. – Laserbeak43 Jan 23 '14 at 06:24
  • Updated once again :) – JleruOHeP Jan 23 '14 at 23:03
  • Wow! Really nice code! Thank you very much, there's so much to C# i need to learn. so much possibility there. – Laserbeak43 Jan 30 '14 at 07:33