ProblemYou want to manipulate images on your forms at runtime without having to load them from accompanying files shipped with your application. SolutionAdd 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. DiscussionAdding 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 windowTo 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 resourcesAt 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 |