void BitmapRequested(CustomMapTileDataSource sender, MapTileBitmapRequestedEventArgs args) { var deferral = args.Request.GetDeferral(); IRandomAccessStreamReference referenceStream; double lat, lon = 0; int pixelX, pixelY = 0; Microsoft.MapPoint.TileSystem.TileXYToPixelXY(args.X, args.Y, out pixelX, out pixelY); Microsoft.MapPoint.TileSystem.PixelXYToLatLong(pixelX, pixelY, args.ZoomLevel, out lat, out lon); BasicGeoposition northWest = new BasicGeoposition { Latitude = lat, Longitude = lon }; Microsoft.MapPoint.TileSystem.PixelXYToLatLong(pixelX + (int)sizeOfMapTile, pixelY + (int)sizeOfMapTile, args.ZoomLevel, out lat, out lon); BasicGeoposition southEast = new BasicGeoposition { Latitude = lat, Longitude = lon }; GeoboundingBox tileBox = new GeoboundingBox(northWest, southEast); if (tileBox.CollidesWith(PathBox)) { if (PathCache.pointInPolygon(northWest.Longitude, northWest.Latitude) && PathCache.pointInPolygon(southEast.Longitude, southEast.Latitude) && PathCache.pointInPolygon(northWest.Longitude, southEast.Latitude) && PathCache.pointInPolygon(southEast.Longitude, northWest.Latitude)) { referenceStream = RandomAccessStreamReference.CreateFromStream(InTile); } else { var cutter = new Windows.Storage.Streams.InMemoryRandomAccessStream(); cutTile(cutter, pixelX, pixelY, args.ZoomLevel); referenceStream = RandomAccessStreamReference.CreateFromStream(cutter); } } else { referenceStream = RandomAccessStreamReference.CreateFromStream(OutTile); } args.Request.PixelData = referenceStream; deferral.Complete(); }