2.3. Use Strongly Typed Resources
In addition to code, .NET assemblies can also contain resourcesembedded binary data such as images and hardcoded strings. Even though .NET has supported a system of resources since Version 1.0, Visual Studio hasn't included integrated design-time support. As a result, developers who need to store image data usually add it to a control that supports it at design time, such as a PictureBox or ImageList. These controls insert the picture data into the application resource file automatically.
Note: Strongly typed resources let you embed static data such as images into your compiled assemblies, and access it easily in your code.
In Visual Studio 2005, it's dramatically easier to add information to the resources file and update it afterward. Even better, you can access this information in a strongly typed fashion from anywhere in your code.
2.3.1. How do I do that?
In order to try using a strongly typed resource of an image in this lab, you need to create a new Windows application before continuing.
To add a resource, start by double-clicking the My Project node in the Solution Explorer. This opens up the application designer, where you can configure a host of application-related settings. Next, click the Resources tab. In the Categories drop-down listbox, select the type of resources you want to see (strings, images, audio, and so on). The string view shows a grid of settings. The image view is a little differentby default, it shows a thumbnail of each picture.
To add a new picture, select the Images category from the drop-down list and then select Add Existing File from the toolbar. Browse to an image file, select it, and click OK. If you don't have an image file handy, try using one from the Windows directory, such as winnt256.bmp (which is included with most versions of Windows).
By default, the resource name has the same name as the file, but you can rename it after adding it. In this example, rename the image to EmbeddedGraphic (as shown in Figure 2-2).
Figure 2-2. Adding a picture as a strongly typed resource
Using a resource is easy. All resources are compiled dynamically into a strongly typed resource class, which you can access through My.Resources. To try out this resource, add a PictureBox control to your Windows form (and keep the default name PictureBox1). Then, add the following code to show the image when the form loads:
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load PictureBox1.Image = My.Resources.EmbeddedGraphic End Sub
Note: The resources class is added in the My Project directory and is given the name Resources.Designer..vb. To see it, you need to choose Project Show All Files. Of course, you should never change this file by hand.
If you run the code, you'll see the image appear on the form. To make sure the image is being extracted from the assembly, try compiling the application and then deleting the image file (the code will still work seamlessly).
When you add a resource in this way, Visual Studio copies the resource to the Resources subdirectory of your application. You can see this directory, along with all the resources it contains, in the Solution Explorer. When you compile your application, all the resources are embedded in the assembly. However, there's a distinct advantage to maintaining them in a separate directory. This way, you can easily update a resource by replacing the file and recompiling the application. You don't need to modify any code. This is a tremendous benefit if you need to update a number of images or other resources at once.
Note: Another advantage of resources is that you can use the same images in multiple controls on multiple different forms, without needing to add more than one copy of the same file.
You can also attach a resource to various controls using the Properties window. For example, when you click the ellipsis (...) in the Properties window next to the Image property for the PictureBox control, a designer appears that lists all the pictures that are available in the application's resources.
2.3.2. What about...
...the ImageList? If you're a Windows developer, you're probably familiar with the ImageList control, which groups together multiple images (usually small bitmaps) for use in other controls, such as menus, toolbars, trees, and lists. The ImageList doesn't use typed resources. Instead, it uses a custom serialization scheme. You'll find that although the ImageList provides design-time support and programmatic access to the images it contains, this access isn't strongly typed.