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?

6
  • 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. Commented Sep 30, 2016 at 3: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? Commented Sep 30, 2016 at 3:39
  • Do the images follow a particular naming pattern? e.g. ImageFile_01.jpg Commented Sep 30, 2016 at 3:42
  • Also, you could take a look at this: stackoverflow.com/questions/8208289/… Commented Sep 30, 2016 at 3: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. Commented Sep 30, 2016 at 3:44

1 Answer 1

2

Give this a shot.

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

foreach (var img in images)
{
    // Do stuff...
}
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.