Getting image from resource file. Create general function to retrieve resources
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.
精彩评论