使用ResourceManager.GetObject将图标对象转换为图像时出错

时间:2017-09-28 23:18:54

标签: c#

我正在阅读位于参考资料中的xml文件中的图标文件名。我能够捕获名为temp的对象,这是我的图标,但是我很难将其转换为图像。有什么建议吗?

    private void GenerateMenusFromXML(XmlNode rootNode, ToolStripMenuItem menuItem)
    {
        // todo implement image generation
        // need to rid hardcoding of the dll filename
        Assembly b = Assembly.LoadFrom(@"MainMenuEnhanced.dll"); 

        ToolStripItem item = null;
        ToolStripSeparator separator = null;
        //
        foreach (XmlNode node in rootNode.ChildNodes)
        {
            if (node.Attributes["Text"].Value == "-")
            {
                separator = new ToolStripSeparator();
                menuItem.DropDownItems.Add(separator);
            }
            else
            {
                item = new ToolStripMenuItem();
                item.Name = node.Attributes["Name"].Value;
                item.Text = node.Attributes["Text"].Value;
                string str = node.Attributes["Image"].Value;
                if (str != "")
                {
                    // Object is an icon....
                    Object test = Properties.Resources.ResourceManager.GetObject(str);
                    // trouble occurs here when trying to cast to an image
                    item.Image = Image test;
                }

                menuItem.DropDownItems.Add(item);

                if (node.Attributes["FormLocation"] != null)
                    item.Tag = node.Attributes["FormLocation"].Value;

                // add an event handler to the menu item added
                if (node.Attributes["OnClick"] != null)
                {
                    FindEventsByName(item, this.Form, true, "MenuItemOn", node.Attributes["OnClick"].Value);
                }

                GenerateMenusFromXML(node, (ToolStripMenuItem)menuItem.DropDownItems[menuItem.DropDownItems.Count - 1]);
            }
        }
    }

0 个答案:

没有答案