1

I am writing a program that calls for me to load in a series of images. I've created a file of images which I've embedded into the C# project. However, I haven't been able to find any suitable answer that will allow me to loop through these images and load them all into a data structure.

Is there a way to do this? I feel like this problem should require a simple fix, and that I may be overthinking it. Any help is appreciated, thanks!

Edit: In the interest of making my question any less vague, I'm trying to access my resource file named "GameBoardImages", which contains all of my images. I can access them one at a time, but have yet to figure out how to implement some mechanism that will allow me to loop through the GameBoardImages file and access and/or collect these images in order to store them into a data structure.

Using the assembly.GetManifestresourceNames() method, I get a list of resources such as: MyNameSpace.GameBoardImages.0800_GameBoardImage.png, MyNameSpace.GameBoardImages.0900_GameBoardImage.png, MyNameSpace.GameBoardIamges.1000_GameBoardImage.png, etc.

How do I loop through the entries in MyNameSpace.GameBoardImages?

AustinC
  • 169
  • 2
  • 7
  • I recently did the exact same thing, read all image files from a directory and all its subdirectories, and add to list (for viewing with WPF). If you can provide a few more details about what you want to achieve exactly, some code, or even where you're getting stuck, I can help you. – Keyur PATEL Sep 30 '16 at 03:36
  • I'm afraid the code I've written is quite unhelpful in regards to my question. I've tried an array of different potential solutions to this issue, all of which ended up not working for me. As I mentioned, I simply need to find a way to loop through all of the images located within a folder which is, itself, embedded into my project, and load those images into an array. (The important bit being, of course, the looping mechanism.) Through what command can I access these images in the folder I've created in such a way that they can be traversed? – AustinC Sep 30 '16 at 03:39
  • Do the images follow a particular naming pattern? e.g. `ImageFile_01.jpg` – Abion47 Sep 30 '16 at 03:42
  • Also, you could take a look at this: http://stackoverflow.com/questions/8208289/list-all-embedded-resources-in-a-folder – Abion47 Sep 30 '16 at 03:42
  • @Abion47 The images are contained in a folder named "GameBoardImages", and follow a numeric naming style: 0470_GameBoardImage.png, 0500_GameBoardImage.png, 1000_GameBoardImage.png, etc. – AustinC Sep 30 '16 at 03:44
  • @Abion47, I've read that question as well, I'm afraid. It allows me to access each image individually, as its own resource, but I can't make out how to loop through all of them from there. – AustinC Sep 30 '16 at 03:45

1 Answers1

2

Give this a shot.

var images = Assembly.GetExecutingAssembly()
                     .GetManifestResourceNames()
                     .Where(x => x.EndsWith("_GameBoardImage.png"))
                     .ToList();

foreach (var img in images)
{
    // Do stuff...
}
Abion47
  • 22,211
  • 4
  • 65
  • 88
  • Brilliant, that worked perfectly! I'll have to do more reading on that .Where(x => x.EndsWith(".")) part; I've never seen that before. Thanks very much for your help! Cheers – AustinC Sep 30 '16 at 04:32