Recipe 10.13. Scrolling Images


Problem

You want to display a picture with full resolution, but you want to let the user scroll around to see all parts of the picture.

Solution

Sample code folder: Chapter 10\ScrollImage

Store the picture in a PictureBox with its SizeMode property set to AutoSize, and place it on a form with its AutoScroll property set to true.

Discussion

To see this demonstration in action, add a PictureBox to a form, set its SizeMode property to AutoSize, and set its Location property to 0,0. Don't worry about its size; the AutoSize setting will take care of that. Change the form's AutoScroll property to true. Now add the following code to the form's class, which loads a picture on startup:

 Private Sub Form1_Load(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles Me.Load    ' ----- Let the user choose a picture.    Dim locateFile As New OpenFileDialog    ' ----- Prompt for the initial file.    locateFile.Filter = "JPG files (*.jpg)|*.jpg"    locateFile.Multiselect = False    If (locateFile.ShowDialog( ) = _          Windows.Forms.DialogResult.OK) Then       ' ----- Show the selected picture.       PictureBox1.Load(locateFile.FileName)       Me.AutoScroll = True    Else       ' ----- Exit the program.       Me.Close( )    End If End Sub 

Run the program, and select a large picture. The scrollbars will automatically appear when needed, as shown in Figure 10-19.

Figure 10-19. Implementing scrollbars to enable scrolling around large images





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