XAML-Map-Control/MapControl/WPF/TileImageLoader.WPF.cs

103 lines
3.5 KiB
C#
Raw Normal View History

2017-08-04 21:38:58 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2018 Clemens Fischer
2017-08-04 21:38:58 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Diagnostics;
using System.IO;
2017-09-05 23:59:22 +02:00
using System.Net;
2017-08-04 21:38:58 +02:00
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace MapControl
{
public partial class TileImageLoader : ITileImageLoader
{
/// <summary>
2017-09-06 20:43:46 +02:00
/// Default folder path where an ObjectCache instance may save cached data,
/// i.e. C:\ProgramData\MapControl\TileCache
2017-08-04 21:38:58 +02:00
/// </summary>
2017-09-06 20:43:46 +02:00
public static string DefaultCacheFolder
{
get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache"); }
}
2017-08-04 21:38:58 +02:00
/// <summary>
/// The ObjectCache used to cache tile images. The default is MemoryCache.Default.
/// </summary>
public static ObjectCache Cache { get; set; } = MemoryCache.Default;
2017-09-05 23:59:22 +02:00
private static int DefaultConnectionLimit
{
get { return ServicePointManager.DefaultConnectionLimit; }
}
2017-08-04 21:38:58 +02:00
private async Task LoadTileImageAsync(Tile tile, Uri uri, string cacheKey)
{
DateTime expiration;
var buffer = GetCachedImage(cacheKey, out expiration);
var loaded = false;
if (buffer == null || expiration < DateTime.UtcNow)
{
try
{
loaded = await ImageLoader.LoadHttpTileImageAsync(uri, async (stream, maxAge) =>
{
await SetTileImageAsync(tile, stream); // create BitmapFrame before caching
2017-08-04 21:38:58 +02:00
SetCachedImage(cacheKey, stream, GetExpiration(maxAge));
});
}
catch (Exception ex)
2017-08-04 21:38:58 +02:00
{
Debug.WriteLine("TileImageLoader: {0}: {1}", uri, ex.Message);
2017-08-04 21:38:58 +02:00
}
}
if (!loaded && buffer != null) // keep expired image if download failed
2017-08-04 21:38:58 +02:00
{
using (var stream = new MemoryStream(buffer))
2017-08-04 21:38:58 +02:00
{
await SetTileImageAsync(tile, stream);
2017-08-04 21:38:58 +02:00
}
}
}
private async Task SetTileImageAsync(Tile tile, MemoryStream stream)
{
var imageSource = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
await tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(imageSource));
}
private static byte[] GetCachedImage(string cacheKey, out DateTime expiration)
{
var buffer = Cache.Get(cacheKey) as byte[];
if (buffer != null && buffer.Length >= 16 &&
Encoding.ASCII.GetString(buffer, buffer.Length - 16, 8) == "EXPIRES:")
{
expiration = new DateTime(BitConverter.ToInt64(buffer, buffer.Length - 8), DateTimeKind.Utc);
}
else
{
expiration = DateTime.MinValue;
}
return buffer;
}
private static void SetCachedImage(string cacheKey, MemoryStream stream, DateTime expiration)
{
stream.Seek(0, SeekOrigin.End);
stream.Write(Encoding.ASCII.GetBytes("EXPIRES:"), 0, 8);
stream.Write(BitConverter.GetBytes(expiration.Ticks), 0, 8);
Cache.Set(cacheKey, stream.ToArray(), new CacheItemPolicy { AbsoluteExpiration = expiration });
}
}
}