Search code examples
c#stringimageresourcesresourcemanager

Need help to use resource manager to load an image from resource with same name as text in textBox


I need to load an image from resources with same name as text in a textBox. I have 2 problems with my actual code:

-The name 'Assembly' doesn't exist in the context

-I don't know how to use my string (index1) to add image from resources to a list.

This is my actual code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Globalization;
using System.Resources;

namespace Booster_pack_2
{
    public partial class Form3 : Form
    {
        public Form3()
        {
        InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            List<Image> Images1 = new List<Image>();
            ResourceManager rm = new ResourceManager("ResourceReader.MyResource", Assembly.GetExecutingAssembly());
            string index1 = textBox1.Text;
            pictureBox1.Image = rm.GetObject(index1) as Image;
            Images1.Add(Booster_pack_2.Properties.Resources.index1);
        }
    }
}

Solution

  • You need System.Reflection in your code to use the Assembly class as suggested by addy2012. As for the list, Properties.Resources has a static ResourceManager in it, use that.

    private void button1_Click(object sender, EventArgs e)
    {
        List<Image> Images1 = new List<Image>();
        ResourceManager rm = new ResourceManager("ResourceReader.MyResource", Assembly.GetExecutingAssembly());
        string index1 = textBox1.Text;
        pictureBox1.Image = rm.GetObject(index1) as Image;
        Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index1));
    }
    

    If both images are the same, you should assign the image to a local variable and use that. Just a suggestion.