2017-10-27 17:15:18 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2018-02-09 17:43:47 +01:00
|
|
|
|
// © 2018 Clemens Fischer
|
2017-10-27 17:15:18 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Windows.Storage;
|
|
|
|
|
|
using Windows.Storage.Streams;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
using Windows.UI.Xaml.Media.Imaging;
|
|
|
|
|
|
using Windows.Web.Http;
|
|
|
|
|
|
using Windows.Web.Http.Headers;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2018-08-08 23:31:52 +02:00
|
|
|
|
public static partial class ImageLoader
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
|
|
|
|
|
public static async Task<ImageSource> LoadLocalImageAsync(Uri uri)
|
|
|
|
|
|
{
|
2018-08-08 23:31:52 +02:00
|
|
|
|
ImageSource imageSource = null;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
|
|
|
|
|
|
|
2018-08-08 23:31:52 +02:00
|
|
|
|
if (File.Exists(path))
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2018-08-08 23:31:52 +02:00
|
|
|
|
var file = await StorageFile.GetFileFromPathAsync(path);
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2018-08-08 23:31:52 +02:00
|
|
|
|
using (var stream = await file.OpenReadAsync())
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2018-08-08 23:31:52 +02:00
|
|
|
|
imageSource = await CreateImageSourceAsync(stream);
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-08-08 23:31:52 +02:00
|
|
|
|
|
|
|
|
|
|
return imageSource;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static async Task<bool> LoadHttpTileImageAsync(Uri uri, Func<IBuffer, TimeSpan?, Task> tileCallback)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var response = await HttpClient.GetAsync(uri))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (IsTileAvailable(response.Headers))
|
|
|
|
|
|
{
|
|
|
|
|
|
var buffer = await response.Content.ReadAsBufferAsync();
|
|
|
|
|
|
|
|
|
|
|
|
await tileCallback(buffer, response.Headers.CacheControl?.MaxAge);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return response.IsSuccessStatusCode;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 23:31:52 +02:00
|
|
|
|
public static async Task<ImageSource> CreateImageSourceAsync(IRandomAccessStream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
var bitmapImage = new BitmapImage();
|
|
|
|
|
|
await bitmapImage.SetSourceAsync(stream);
|
|
|
|
|
|
return bitmapImage;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static async Task<InMemoryRandomAccessStream> GetResponseStreamAsync(IHttpContent content)
|
|
|
|
|
|
{
|
|
|
|
|
|
var stream = new InMemoryRandomAccessStream();
|
|
|
|
|
|
await content.WriteToStreamAsync(stream);
|
|
|
|
|
|
stream.Seek(0);
|
|
|
|
|
|
return stream;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-27 17:15:18 +02:00
|
|
|
|
private static bool IsTileAvailable(HttpResponseHeaderCollection responseHeaders)
|
|
|
|
|
|
{
|
|
|
|
|
|
return !responseHeaders.TryGetValue("X-VE-Tile-Info", out string tileInfo) || tileInfo != "no-tile";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|