c#调用导入的资源是不是只能用Properties.Resources.导入资源的名称????

2025-02-24 06:06:24
推荐回答(1个)
回答1:

理论上是可以的,本质上那是一个类,用反射包装一下应该就可以获取了。你先试试。

        private void button1_Click(object sender, EventArgs e)
        {
            //this.pictureBox1.Image = Resources.P1;
            //this.pictureBox1.Image = Resources.P2;

            //下面的语句也可以成功
            this.pictureBox1.Image = GetImageByIndex(1);
        }

        //改成你自己的命名空间
        //using WindowsFormsApplication18.Properties;
        //using System.Reflection;
        Image GetImageByIndex(int index)
        {
            Type t = typeof(Resources);
            string pn = "P" + index.ToString();
            PropertyInfo pi = t.GetProperty(pn, 
                BindingFlags.NonPublic | BindingFlags.Static);
            object o = pi.GetValue(null, null);
            return (Image)o;
        }