VB.NET•Display From Multiple TerraService Tiles

Listing 2. Code for generating display images stitches together adjacent tiles cropped to XOffset and YOffset coordinates into bitmaps of the desired size. You need 12 to 20 invocations of the TerraService's GetTile Web method to complete a 640-by-480-pixel image. The GetTileImage function's code shown doesn't include the instructions that return image properties for display in the ImageData dialog.

Private Function GetTileImage( _
   ByVal TS As TS.TerraService, _
   ByVal ptCity As TS.LonLatPt, _
   ByVal intWidth As Integer, _
   ByVal intHeight As Integer) As Bitmap
   'Return an AreaBoundingBox by invoking the 
   'GetAreaFromPt Web method.
   'Retrieve the required tiles and draw the image
   Try
      Dim abbImage As New TS.AreaBoundingBox
      If blnIsTopoMap Then
         abbImage = TS.GetAreaFromPt( _
            ptCity, _tsTheme.Topo, _
            _tsScale, intWidth, intHeight)
      Else
         abbImage = TS.GetAreaFromPt( _
            ptCity, tsTheme.Photo, _
            tsScale, intWidth, intHeight)
      End If
      dblLatPoint = ptCity.Lat
      dblLonPoint = ptCity.Lon
      intOffsetX = _
         abbImage.NorthWest.Offset.XOffset
      intOffsetY = _
         abbImage.NorthWest.Offset.YOffset
      'Generate an image that fits the display 
      'region (640 x 480)
      Dim bmpTiles As New Bitmap(intWidth, _
         intHeight, pixFormat)
      Dim grfImage As Graphics = _
         Graphics.FromImage(bmpTiles)

      'Get the range of Tile X eastings and Y northings
      Dim x1 As Integer = _
         abbImage.NorthWest.TileMeta.Id.X
      Dim y1 As Integer = _
         abbImage.NorthWest.TileMeta.Id.Y
      Dim x2 As Integer = _
         abbImage.NorthEast.TileMeta.Id.X
      Dim y2 As Integer = _
         abbImage.SouthWest.TileMeta.Id.Y

      Dim x As Integer
      For x = x1 To x2
         'Loop west to east
         Dim y As Integer
         intHor = 0
         For y = y1 To y2 Step -1
            'Loop north to south
            Dim tilID As TS.TileId = abbImage.NorthWest. _
               TileMeta.Id
            tilID.X = x
            tilID.Y = y
            Dim tilImage As Image = Image.FromStream(New _
               MemoryStream( _
               TS.GetTile(tilID)))
            'Get the image by retrieving 
            'all tiles specified and 
            'applying X and Y offsets to the tiled image
            grfImage.DrawImage(tilImage, (x - x1) * _
               tilImage.Width - abbImage.NorthWest. _
               Offset.XOffset, (y1 - y) * tilImage.Height _
               - abbImage.NorthWest. _
               Offset.YOffset, tilImage.Width, _
               tilImage.Height)
            tilImage.Dispose()
         Next y
      Next x
      Return bmpTiles
   Catch exc As Exception
      MessageBox.Show(exc.Message + exc.StackTrace, _
         "Error in GetTileImage Function")
   End Try
End Function