How to programmatically access images in a resource file?

I have 30 PNGs in the resource file, and I would like them to repeat around the timer. This timer sets the background image of the form to the next PNG in sequence to create a basic animation.

I cannot find an easy way to list the resource file and get the actual images. I also try to ensure that image links are not fixed to their file names, so updating the image name in the resource file would not require me to update this section of code.

Notes:

  • Images inside the resource file are called sequentially ('image001.png', 'image002.png', ...).
  • This resource file is used exclusively for storing these images.
+5
source share
4 answers
    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
        }

    }
+2
source
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
}

Get the name of all built-in resources:

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

Also, check the example in the Form1_Load function.

+1
source

CodeProject, - , . , .

, , , .

+1

All Articles