InvertedAcceleration
InvertedAcceleration

Reputation: 11003

How to programatically access images in a resource file?

I have 30 PNGs in a resource file and I would like to iterate around them inside a timer. This timer sets the form's background image to the next PNG in the sequence to produce a basic animation.

I can't find an easy way to enumerate the resource file and get at the actual images. I am also keen to keep the references to the images not fixed to their filenames so that updating the naming of the images within the Resource File would not require me to update this section of code.

Notes:

Upvotes: 5

Views: 8297

Answers (4)

Fredou
Fredou

Reputation: 20100

    private void Form1_Load(object sender, EventArgs e)
    {
        var list = WindowsFormsApplication1.Properties.Resources.ResourceManager.GetResourceSet(new System.Globalization.CultureInfo("en-us"), true, true);
        foreach (System.Collections.DictionaryEntry img in list)
        {
            System.Diagnostics.Debug.WriteLine(img.Key);
            //use img.Value to get the bitmap
        }

    }

Upvotes: 2

SwDevMan81
SwDevMan81

Reputation: 49978

Assembly asm = Assembly.GetExecutingAssembly();
for(int i = 1; i <= 30; i++)
{
  Stream file = asm.GetManifestResourceStream("NameSpace.Resources.Image"+i.ToString("000")+".png");
  // Do something or store the stream
}

To Get the name of all embedded resources:

string[] resourceNames = Assembly.GetManifestResourceNames();
foreach(string resourceName in resourceNames)
{
    System.Console.WriteLine(resourceName);
}

Also, check out the example in the Form1_Load function.

Upvotes: 1

t0mm13b
t0mm13b

Reputation: 34592

Here is a nice tutorial on how to extract embedded resources here on CodeProject, and here is how to use an image as alpha-blended, and shows how to load it into an image list. Here's a helper class to make loading embedded resources easier here.

Hope this helps, Best regards, Tom.

Upvotes: 1

Thorsten79
Thorsten79

Reputation: 10128

How about this forum answer that uses GetManifestResourceStream() ?

Upvotes: 0

Related Questions