2017-10-27 17:15:18 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2019-03-27 18:39:59 +01:00
|
|
|
|
// © 2019 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;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Windows.Storage;
|
|
|
|
|
|
using Windows.Storage.Streams;
|
2019-06-13 21:38:01 +02:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
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
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
public static async Task<ImageSource> LoadImageAsync(IRandomAccessStream stream)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2018-12-09 17:14:32 +01:00
|
|
|
|
var image = new BitmapImage();
|
|
|
|
|
|
await image.SetSourceAsync(stream);
|
|
|
|
|
|
return image;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
public static async Task<ImageSource> LoadImageAsync(byte[] buffer)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
using (var stream = new InMemoryRandomAccessStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
await stream.WriteAsync(buffer.AsBuffer());
|
|
|
|
|
|
stream.Seek(0);
|
|
|
|
|
|
|
2018-08-16 19:43:16 +02:00
|
|
|
|
return await LoadImageAsync(stream);
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
public static async Task<ImageSource> LoadImageAsync(string path)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
ImageSource image = null;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
|
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
var file = await StorageFile.GetFileFromPathAsync(Path.GetFullPath(path));
|
2018-08-15 23:29:03 +02:00
|
|
|
|
|
|
|
|
|
|
using (var stream = await file.OpenReadAsync())
|
|
|
|
|
|
{
|
2018-12-09 17:14:32 +01:00
|
|
|
|
image = await LoadImageAsync(stream);
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-09 17:14:32 +01:00
|
|
|
|
return image;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
private static async Task<ImageSource> LoadImageAsync(IHttpContent content)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var stream = new InMemoryRandomAccessStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
await content.WriteToStreamAsync(stream);
|
|
|
|
|
|
stream.Seek(0);
|
|
|
|
|
|
|
|
|
|
|
|
return await LoadImageAsync(stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
internal class HttpBufferResponse
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
public readonly IBuffer Buffer;
|
|
|
|
|
|
public readonly TimeSpan? MaxAge;
|
|
|
|
|
|
|
|
|
|
|
|
public HttpBufferResponse(IBuffer buffer, TimeSpan? maxAge)
|
|
|
|
|
|
{
|
|
|
|
|
|
Buffer = buffer;
|
|
|
|
|
|
MaxAge = maxAge;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal static async Task<HttpBufferResponse> LoadHttpBufferAsync(Uri uri)
|
|
|
|
|
|
{
|
|
|
|
|
|
HttpBufferResponse response = null;
|
2018-08-09 18:21:16 +02:00
|
|
|
|
|
|
|
|
|
|
try
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
using (var responseMessage = await HttpClient.GetAsync(uri))
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
if (responseMessage.IsSuccessStatusCode)
|
2018-08-09 18:21:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
IBuffer buffer = null;
|
|
|
|
|
|
TimeSpan? maxAge = null;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
if (ImageAvailable(responseMessage.Headers))
|
2018-08-09 18:21:16 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
buffer = await responseMessage.Content.ReadAsBufferAsync();
|
|
|
|
|
|
maxAge = responseMessage.Headers.CacheControl?.MaxAge;
|
2018-08-09 18:21:16 +02:00
|
|
|
|
}
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
response = new HttpBufferResponse(buffer, maxAge);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)responseMessage.StatusCode, responseMessage.ReasonPhrase);
|
2018-08-09 18:21:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
return response;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
private static bool ImageAvailable(HttpResponseHeaderCollection responseHeaders)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
|
|
|
|
|
return !responseHeaders.TryGetValue("X-VE-Tile-Info", out string tileInfo) || tileInfo != "no-tile";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|