11.10 Using ResourceReader to Display an Image Resource

 <  Day Day Up  >  

11.10 Using ResourceReader to Display an Image Resource

You want to load an Image from a satellite assembly to display it on a Windows Form.


Technique

To load an image within a satellite assembly, create an instance of the ResourceManager class passing the root name of the resources and a reference to the Assembly object where the resources are defined. In most cases, you can simply use the GetExecutingAssembly static method defined in the Assembly class if you are retrieving resources within the same application:

 
 // create a resourcemanager to load satellite assembly ResourceManager resMan = new ResourceManager(     "_9_SatelliteAssemblyClient.MyResources",     Assembly.GetExecutingAssembly() ); 

Once you create the ResourceManager , you can use its various methods to extract resources using the name of the resource given when you created the resource file. To extract a string, use the GetString method, passing the string identifier as the parameter. To extract any other data type within the resource file, use the GetObject method. It returns a System.Object , which you can then cast to the appropriate data type such as Image for images:

 
 // set picture box pbFlag.Image = (Image) resMan.GetObject( "flag" ); // set label lblHello.Text = resMan.GetString( "Hello" ); 

Comments

One common pitfall with creating resource files using the methods demonstrated in this and the last recipe is filenames. When you create a ResourceManager object, you need to pass the root name of the resource file to use. The confusion arises from the name given to the .resx file that was added to the project and the name of the .resources file that the compiler automatically generates. When the embedded resource is built into a satellite assembly, a .resources file is created whose filename is a combination of the project name and the original name given to the .resx file. In other words, if you create a resource file named MyResources.en-GB.resx within a project named MyProject , the generated .resources file is MyProject.MyResources.en-GB.resources . The base name for this resource is everything in the filename except for the ISO culture identifiers and file extension, which for this example would be MyProject.MyResources .

Once you create the ResourceManager object, retrieving objects from the resource file is trivial. Based on the CurrentUICulture , the ResourceManager handles the job of finding the correct satellite assembly and retrieving the requested resource. All that you need is the associated GetString and GetObject method calls to retrieve those resources. In the code associated with this recipe, you can see how different culture-based resources are used at runtime to extract a given country's animated flag and a localized Hello string.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net