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
|
|
|
|
|
2018-08-09 18:58:47 +02:00
|
|
|
|
try
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2018-08-09 18:58:47 +02:00
|
|
|
|
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2018-08-09 18:58:47 +02:00
|
|
|
|
if (File.Exists(path))
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2018-08-09 18:58:47 +02:00
|
|
|
|
var file = await StorageFile.GetFileFromPathAsync(path);
|
|
|
|
|
|
|
|
|
|
|
|
using (var stream = await file.OpenReadAsync())
|
|
|
|
|
|
{
|
|
|
|
|
|
imageSource = await CreateImageSourceAsync(stream);
|
|
|
|
|
|
}
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-08-09 18:58:47 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
|
|
|
|
|
}
|
2018-08-08 23:31:52 +02:00
|
|
|
|
|
|
|
|
|
|
return imageSource;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-09 18:21:16 +02:00
|
|
|
|
public static async Task<Tuple<IBuffer, TimeSpan?>> LoadHttpBufferAsync(Uri uri)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2018-08-09 18:21:16 +02:00
|
|
|
|
Tuple<IBuffer, TimeSpan?> result = null;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2018-08-09 18:21:16 +02:00
|
|
|
|
using (var response = await HttpClient.GetAsync(uri))
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2018-08-09 18:21:16 +02:00
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
IBuffer buffer = null;
|
|
|
|
|
|
TimeSpan? maxAge = null;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2018-08-09 18:21:16 +02:00
|
|
|
|
if (IsTileAvailable(response.Headers))
|
|
|
|
|
|
{
|
|
|
|
|
|
buffer = await response.Content.ReadAsBufferAsync();
|
|
|
|
|
|
maxAge = response.Headers.CacheControl?.MaxAge;
|
|
|
|
|
|
}
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2018-08-09 18:21:16 +02:00
|
|
|
|
result = new Tuple<IBuffer, TimeSpan?>(buffer, maxAge);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
2018-08-09 18:21:16 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-09 18:21:16 +02:00
|
|
|
|
private static async Task<ImageSource> CreateImageSourceAsync(IHttpContent content)
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2018-08-09 18:21:16 +02:00
|
|
|
|
using (var stream = new InMemoryRandomAccessStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
await content.WriteToStreamAsync(stream);
|
|
|
|
|
|
stream.Seek(0);
|
|
|
|
|
|
return await CreateImageSourceAsync(stream);
|
|
|
|
|
|
}
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|