Image and File Resources


The Visual Studio 2005 Resource Editor maintains string, bitmap, icon, audio, file, and other resources in a resx file. You've seen how to add a string to a resource file. To add an image, select "Images" from the Categories combo box (entries in the combo box are bold when there are one or more entries of that type). The main part of the Resource Editor showing the strings is replaced with an area that shows all the image resources. It is blank at this stage. To add an image from an existing file, drop down the Add Resource button and select Add Existing File...; use the "Add existing file to resources" file open dialog to locate the image you want to add. Let's say that you want to show the national flag of the selected culture as the form's background so that the user has very clear feedback of the currently selected culture. Add the U.S. national flag to Form1Resources.resx and ensure that the image is called "NationalFlag" (see Figure 3.8).

Figure 3.8. Adding an Image Using the Visual Studio 2005 Resource Editor


Repeat the process and add the French national flag to Form1Resources.fr. resx; ensure that it is called "NationalFlag". Thus, all resources have an image entry with the same name. If you give the images country-specific names, such as USNationalFlag and FrenchNationalFlag, the resource names will not be polymorphic. To use the bitmap as the form's background, set Form1.Back-groundImageLayout to Stretch and add the following line to the end of the Form1 constructor:

 BackgroundImage = (Bitmap) resourceManager.GetObject("NationalFlag"); 


ResourceManager.GetObject retrieves an object resource in the same way that ResourceManager.GetString retrieves a string resource. We know that the resource is a bitmap, so we cast it to a Bitmap and assign it directly to the form's BackgroundImage. Of course, when the current thread's CurrentUICulture changes, there is no CurrentUICultureChanged event that we can hook into to get notification that it has changed, so we need to add this same line immediately after any line that changes the CurrentUICulturethat is, we have to add it to the end of both radio buttons' CheckChanged events. Now the form will always show the national flag of the selected CurrentUICulture.

To add a text file as opposed to an image, you follow a similar process. If the file already exists, click the Add Resource button in the Resource Editor, select Add Existing File..., and enter the text file to add. If the file does not already exist, click the Add Resource button, select Add New Text File, and enter a name for the resource. To edit the text file, double-click its resource icon. To retrieve the contents of the text file, use the ResourceManager.GetString method, just as you would for getting a resource string.

Adding Images and Files in Visual Studio 2003

Adding images to a resx file in Visual Studio 2003 requires more effort than for Visual Studio 2005. The Visual Studio 2003 Resource Editor does not offer any facilities for reading image files. Two solutions to this problem exist:

  1. Use file references in the Visual Studio 2003 Resource Editor

  2. Embed the image in the resx file using ResEditor.exe

The first solution lies in manually mimicking the functionality of the Visual Studio 2005 Resource Editor. The Visual Studio 2005 Resource Editor creates "file references" to the image files that it adds to resx files. That is, the image file is referenced by the resx file (instead of the image being embedded in the resx file). The reference is achieved using the ResXfileref class, which is present in both the .NET Framework 1.1 and 2.0. To add the NationalFlag image to a resx file using the Visual Studio 2003 Resource Editor, add a new resource entry called "NationalFlag" and set its type to "System.Resources.ResXFileRef, System.Windows.Forms, Version= 1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089". This indicates that the value of the resource entry is a file reference, not the actual value. Now set the value to "C:\Books\I18N\Tests\VS2003\Windows Application1\NationalFlag.bmp;System.Drawing.Bitmap, System.Drawing, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". The value includes the filename and the type of the resource (i.e., System.Drawing. Bitmap). The entry in the resx file looks like this:

 <data name="NationalFlag" type="System.Resources.ResXfileref, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">     <value>     C:\Books\I18N\Tests\VS2003\WindowsApplication1\NationalFlag.bmp;     System.Drawing.Bitmap, System.Drawing, Version=1.0.5000.0,     Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a     </value> </data> 


In this example, the file includes an absolute path. This isn't strictly necessary, but you might find it helpful. Without an absolute path, both Visual Studio 2003 and ResGen assume the current folder. For Visual Studio 2003, this is the devenv.exe folder, which is typically \Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE. Consequently, without an absolute path, you must place the referenced files in this folder, which is a poor choice for files that are specific to a single application.

The second solution is to embed the image in the resx file using one of the examples in the .NET Framework SDK, ResEditor.exe, which allows all objects in resx and resources files to be maintained. The source for ResEditor is in <SDK>\v1.1\ Samples\Tutorials\resourcesandlocalization\reseditor (where <SDK> is the location of the SDK, probably \Program Files\Microsoft Visual Studio .NET 2003\SDK). Build ResEditor using the build.bat file found there. Run Res Editor.exe; select File, Open; and open Form1Resources.resx. In the TextBox to the left of the Add button, enter "NationalFlag"and click Add. Now click the ellipses in the PropertyGrid and enter the name of the file. The bitmap is added to the resx file (see Figure 3.9).

Figure 3.9. Adding an Image Using the .NET Framework 1.1 SDK ResEditor.exe





.NET Internationalization(c) The Developer's Guide to Building Global Windows and Web Applications
.NET Internationalization: The Developers Guide to Building Global Windows and Web Applications
ISBN: 0321341384
EAN: 2147483647
Year: 2006
Pages: 213

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