Recipe 10.15. Using Resource Images


Problem

You want to manipulate images on your forms at runtime without having to load them from accompanying files shipped with your application.

Solution

Add the images to the application's resources, then load them into controls or process them as needed by accessing them directly from the My.Resources object.

Discussion

Adding pictures, icons, strings, or other items to your application's resources is very straightforward and easy to do in Visual Basic 2005. This recipe shows the steps involved for adding images, but the process easily extends to other types of resources.

Resource items are maintained at design time by double-clicking My Project in the Solution Explorer list and selecting the Resources tab. Figure 10-21 shows an example set of image resources as they appear in the Resources maintenance dialog.

Figure 10-21. Resource-maintenance tasks are carried out on the Resources tab of the Project Properties window


To add a new picture to the collection of images, click the Add Resources pull-down menu, open the New Image submenu, and select JPEG Image, as shown in Figure 10-22.

Figure 10-22. Adding new images to your resources


At runtime, the images stored in your application's resources are referenced by name. For example, the following code loads either the Goose or the Teton image into PictureBox1, based on the current state of the static Boolean variable showTheGoose:

 Private Sub ShowImage(ByVal useTheGoose As Boolean)    ' ----- Goose or Teton: hard choice!    If (useTheGoose = True) Then       PictureBox1.Image = My.Resources.Goose    Else       PictureBox1.Image = My.Resources.Teton    End If End Sub 




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

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