开发者

Getting image from resource file. Create general function to retrieve resources

开发者 https://www.devze.com 2023-04-01 12:29 出处:网络
I have a DLL, that stores the images for our project.(C# 4.0, 开发者_Python百科VS2010). In the DLL there is a resource file, I\'m using -

I have a DLL, that stores the images for our project.(C# 4.0, 开发者_Python百科VS2010). In the DLL there is a resource file, I'm using -

public Image Get1()
{
   return DM.DMReourceLib._1; 
}

in order to access the image _1. This way I'll end up writing 1200 get's functions, one for each image. I'm looking for a way to do

public Image GetImage(string name)
{
   return DM.DMresourceLib.name;
}

10x


Take a look how designer class was generated. Maybe it can help you:

internal static System.Drawing.Bitmap price {
        get {
            object obj = ResourceManager.GetObject("price", resourceCulture);
            return ((System.Drawing.Bitmap)(obj));
        }
    }


var field = typeof(DM.DMresourceLib).GetField(name);
return (Image)field.GetValue(DM.DMresourceLib);

...or something like that. (If those are properties use GetProperty of course)


Use Reflection:

public Image GetImage(string name) 
{   
    var field = DM.DMresourceLib.GetType().GetField(name);             

    return (Image)field.GetValue(DM.DMresourceLib); 
}


Image ImageFromResource = (Image) Properties.Resources.ResourceManager.GetObject("RecordImage");

Please write the Namespace while posting code. Even what reference to add, if needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号