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
      Dim abbImage As New TS.AreaBoundingBox
      If blnIsTopoMap Then
         abbImage = TS.GetAreaFromPt( _
            ptCity, _tsTheme.Topo, _
            _tsScale, intWidth, intHeight)
         abbImage = TS.GetAreaFromPt( _
            ptCity, tsTheme.Photo, _
            tsScale, intWidth, intHeight)
      End If
      dblLatPoint = ptCity.Lat
      dblLonPoint = ptCity.Lon
      intOffsetX = _
      intOffsetY = _
      'Generate an image that fits the display 
      'region (640 x 480)
      Dim bmpTiles As New Bitmap(intWidth, _
         intHeight, pixFormat)
      Dim grfImage As Graphics = _

      'Get the range of Tile X eastings and Y northings
      Dim x1 As Integer = _
      Dim y1 As Integer = _
      Dim x2 As Integer = _
      Dim y2 As Integer = _

      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. _
            tilID.X = x
            tilID.Y = y
            Dim tilImage As Image = Image.FromStream(New _
               MemoryStream( _
            '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, _
         Next y
      Next x
      Return bmpTiles
   Catch exc As Exception
      MessageBox.Show(exc.Message + exc.StackTrace, _
         "Error in GetTileImage Function")
   End Try
End Function