/// <summary> Retrieves the image of a certain map tile and returns it as image stream. </summary> /// <param name="x"> X coordinate of the tile. </param> /// <param name="y"> Y coordinate of the tile. </param> /// <param name="zoom"> Zoom factor of the tile. </param> /// <returns> Image stream of the specified tile. </returns> public Stream GetImageStream(int x, int y, int zoom) { string tmpUrl = metaInfo.ImageUrl; tmpUrl = tmpUrl.Replace("{subdomain}", metaInfo.ImageUrlSubDomains[(x ^ y) % metaInfo.ImageUrlSubDomains.Length]); tmpUrl = tmpUrl.Replace("{quadkey}", GeoTransform.TileXYToQuadKey(x, y, zoom)); if (tmpUrl.Contains("{culture}")) { tmpUrl = tmpUrl.Replace("{culture}", Thread.CurrentThread.CurrentUICulture.Name.ToLower()); } // append key if (!string.IsNullOrEmpty(metaInfo.Key)) { tmpUrl = tmpUrl + "&key=" + metaInfo.Key; } // n=z will return 404 if the tile is not available instead of the "no imagery" image. tmpUrl = tmpUrl + "&n=z"; try { var request = (HttpWebRequest)WebRequest.Create(tmpUrl); request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable); request.KeepAlive = false; return(request.GetResponse().GetResponseStream()); } catch (Exception exception) { return(TileExceptionHandler.RenderException(exception, 256, 256)); } }
/// <summary> Retrieves the image of a certain map tile and returns it as image stream. </summary> /// <param name="x"> X coordinate of the tile. </param> /// <param name="y"> Y coordinate of the tile. </param> /// <param name="zoom"> Zoom factor of the tile. </param> /// <returns> Image stream of the specified tile. </returns> public Stream GetImageStream(int x, int y, int zoom) { string tmpUrl = metaInfo.ImageUrl; tmpUrl = tmpUrl.Replace("{subdomain}", metaInfo.ImageUrlSubDomains[(x ^ y) % metaInfo.ImageUrlSubDomains.Length]); tmpUrl = tmpUrl.Replace("{quadkey}", GeoTransform.TileXYToQuadKey(x, y, zoom)); if (tmpUrl.Contains("{culture}")) { tmpUrl = tmpUrl.Replace("{culture}", Thread.CurrentThread.CurrentUICulture.Name.ToLower()); } // append key if (!string.IsNullOrEmpty(metaInfo.Key)) { tmpUrl = tmpUrl + "&key=" + metaInfo.Key; } // n=z will return 404 if the tile is not available instead of the "no imagery" image. tmpUrl = tmpUrl + "&n=z"; try { var request = (HttpWebRequest)WebRequest.Create(tmpUrl); request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable); request.KeepAlive = false; request.Proxy.Credentials = CredentialCache.DefaultCredentials; return(request.GetResponse().GetResponseStream()); } catch (WebException webException) { logger.Writeline(TraceEventType.Error, "WebException occured :" + Environment.NewLine + "Exception Message : " + webException.Message); logger.Writeline(TraceEventType.Error, "URL :" + tmpUrl); logger.Writeline(TraceEventType.Error, string.Format("WebException Status : {0}", webException.Status)); if (webException.Status == WebExceptionStatus.ProtocolError) { logger.Writeline(TraceEventType.Error, string.Format("Status Code : {0}", ((HttpWebResponse)webException.Response).StatusCode)); logger.Writeline(TraceEventType.Error, string.Format("Status Description : {0}", ((HttpWebResponse)webException.Response).StatusDescription)); } return(TileExceptionHandler.RenderException(webException, 256, 256)); } catch (Exception exception) { logger.Writeline(TraceEventType.Error, "Exception occured : " + Environment.NewLine + exception.Message); return(TileExceptionHandler.RenderException(exception, 256, 256)); } }