检索资源名称为变量的图像资源

时间:2014-02-05 17:33:51

标签: c# asp.net

我有一个自托管的web api控制台应用。它包括一个链接的资源图像,我可以访问:

Bitmap imgBitmap = Properties.Resources.img2

但是我想将其作为变量访问。即资源的名称将作为包含字符串的变量提供:“img2”

关于这个问题还有其他问题,但它们似乎没有用,或者有足够的信息让我知道如何去做。 E.g:

Retrieve image resource using string variable in foreach loop

所以我尝试过(但我不确定如何指定这个 - 我尝试了几种变体)......

        ResourceManager rm = new ResourceManager(
            "Properties.Resources",
            typeof(Properties.Resources).Assembly);

        var v1 = rm.GetObject("img2");

这给了我运行时错误......

“无法找到适合指定文化或中性文化的任何资源。确保在编译时将”Properties.Resources.resources“正确嵌入或链接到程序集”OwinWebApi2“中,或者所需的所有附属程序集都是可加载和完全签名。“

我的Resources.Designer.cs看起来像这样......

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18408
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Owin_Test1.Properties {
    using System;


    /// <summary>
    ///   A strongly-typed resource class, for looking up localized strings, etc.
    /// </summary>
    // This class was auto-generated by the StronglyTypedResourceBuilder
    // class via a tool like ResGen or Visual Studio.
    // To add or remove a member, edit your .ResX file then rerun ResGen
    // with the /str option, or rebuild your VS project.
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    internal class Resources {

        private static global::System.Resources.ResourceManager resourceMan;

        private static global::System.Globalization.CultureInfo resourceCulture;

        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal Resources() {
        }

        /// <summary>
        ///   Returns the cached ResourceManager instance used by this class.
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Owin_Test1.Properties.Resources", typeof(Resources).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }

        /// <summary>
        ///   Overrides the current thread's CurrentUICulture property for all
        ///   resource lookups using this strongly typed resource class.
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }

        /// <summary>
        ///   Looks up a localized resource of type System.Drawing.Bitmap.
        /// </summary>
        internal static System.Drawing.Bitmap img2 {
            get {
                object obj = ResourceManager.GetObject("img2", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to &lt;!DOCTYPE html&gt;
        ///
        ///&lt;html lang=&quot;en&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
        ///&lt;head&gt;
        ///    &lt;meta charset=&quot;utf-8&quot; /&gt;
        ///    &lt;title&gt;&lt;/title&gt;
        ///&lt;/head&gt;
        ///&lt;body&gt;
        ///    Page1, now in resource editor
        ///&lt;/body&gt;
        ///&lt;/html&gt;.
        /// </summary>
        internal static string Page1 {
            get {
                return ResourceManager.GetString("Page1", resourceCulture);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

创建资源管理器时需要包含完整的命名空间。而不是

new ResourceManager(
    "Properties.Resources",
    typeof(Properties.Resources).Assembly);

应该是

new ResourceManager(
    "Owin_Test1.Properties.Resources",
    typeof(Properties.Resources).Assembly);

如果您查看ResourceManager的{​​{1}}属性,可以看到。