Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Icons from resource name

Status
Not open for further replies.

Echilon

Programmer
Feb 22, 2007
54
GB
I'm trying to access image resources in my application, but I can't the resources from the application, I just end up with null. I've added the resources in the properties section in visual studio 2008, and the code I'm using is
private ResourceManager res = new ResourceManager("MyNameSpace.Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
Image myImage = (Image)res.GetObject("myImage.png");
But myImage is always null. I know I could use MyNameSpace.Properties.Resources.myImage.png, but I need to get the resource from a string.
 
I have used resources similarly recently, and they work fine.

First, make sure the resource name is correct. Keep in mind that the name isn't necessarily the file name. For instance, the name may be "myImage" instead of "myImage.png".

Secondly, make sure you are casting it to the correct type. I checked for the type using the following:

Code:
Object obj = res.GetObject( "res_name" );
if ( obj.GetType() == typeof( Image ) )
  myImage = (Image)obj;
else
  throw new Exception( "Type error" )

|| ABC
 
That was it. I needed to leave off the extension. Problem solved. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top