private bool TryGetTile(TileCoordinates key, out BaseMapTileProxy proxy) { // queue new load if (!_tiles.TryGetValue(key, out proxy)) { var tile = new BaseMapTileProxy(key) { State = BaseMapTileProxy.TileState.Loading, ZoomLevel = zoomLevel }; _tiles.Add(key, tile); _loadQueue.Enqueue(tile); return(false); } // requeue failed if (proxy.State == BaseMapTileProxy.TileState.Failed || proxy.State == BaseMapTileProxy.TileState.Empty) { _loadQueue.Enqueue(proxy); } return(true); }
/// <summary> /// Handle the completion of our map tile download /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TileDownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) { concurrentDownloads--; WebClientDownloadState state = null; try { state = (WebClientDownloadState)e.UserState; BaseMapTileProxy tile = (BaseMapTileProxy)state.Data; if (e.Cancelled || e.Error != null) { var webException = e.Error as WebException; if (webException != null && webException.Response is System.Net.HttpWebResponse) { if ((((System.Net.HttpWebResponse)webException.Response).StatusCode == HttpStatusCode.NotFound)) { tile.State = BaseMapTileProxy.TileState.NotFound; } } else { tile.State = BaseMapTileProxy.TileState.Failed; } } else { byte[] InBytes = e.Result; if (!string.IsNullOrEmpty(MM_Repository.OverallDisplay.MapTileCache)) { string filename = tile.GetFileName(); if (!Directory.Exists(Path.GetDirectoryName(filename))) { Directory.CreateDirectory(Path.GetDirectoryName(filename)); } File.WriteAllBytes(filename, InBytes); tile.Bitmap = Surface.RenderTarget2D.LoadBitmapFromFile(filename); tile.State = BaseMapTileProxy.TileState.Loaded; } else { using (MemoryStream mS = new MemoryStream(InBytes)) { var bmp = ((System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(mS)); tile.Bitmap = bmp.ToDxBitmap(this.Surface.RenderTarget2D); bmp.Dispose(); tile.State = BaseMapTileProxy.TileState.Loaded; } } } } catch (Exception ex) { Console.WriteLine("Error retrieving map tile: " + ex.Message); } finally { if (state != null && state.Client != null) { state.Client.Dispose(); } } }