2017-10-27 17:15:18 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2021-01-13 21:19:27 +01:00
|
|
|
|
// © 2021 Clemens Fischer
|
2017-10-27 17:15:18 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2019-06-13 21:38:01 +02:00
|
|
|
|
using System.Windows.Media;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2018-08-08 23:31:52 +02:00
|
|
|
|
public static partial class ImageLoader
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2021-11-28 23:50:27 +01:00
|
|
|
|
public static Task<ImageSource> LoadImageAsync(Stream stream)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2021-11-28 23:50:27 +01:00
|
|
|
|
return Task.Run(() => LoadImage(stream));
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-28 23:50:27 +01:00
|
|
|
|
public static Task<ImageSource> LoadImageAsync(byte[] buffer)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2021-11-28 23:50:27 +01:00
|
|
|
|
return Task.Run(() =>
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2021-11-28 23:50:27 +01:00
|
|
|
|
using (var stream = new MemoryStream(buffer))
|
|
|
|
|
|
{
|
|
|
|
|
|
return LoadImage(stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-28 23:50:27 +01:00
|
|
|
|
public static Task<ImageSource> LoadImageAsync(string path)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2021-11-28 23:50:27 +01:00
|
|
|
|
return Task.Run(() =>
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2021-11-28 23:50:27 +01:00
|
|
|
|
if (!File.Exists(path))
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2021-11-28 23:50:27 +01:00
|
|
|
|
return null;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-28 23:50:27 +01:00
|
|
|
|
using (var stream = File.OpenRead(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
return LoadImage(stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-28 23:50:27 +01:00
|
|
|
|
private static ImageSource LoadImage(Stream stream)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2021-11-28 23:50:27 +01:00
|
|
|
|
var bitmapImage = new BitmapImage();
|
2019-06-15 01:39:07 +02:00
|
|
|
|
|
2021-11-28 23:50:27 +01:00
|
|
|
|
bitmapImage.BeginInit();
|
|
|
|
|
|
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
|
|
|
|
|
|
bitmapImage.StreamSource = stream;
|
|
|
|
|
|
bitmapImage.EndInit();
|
|
|
|
|
|
bitmapImage.Freeze();
|
2019-06-15 01:39:07 +02:00
|
|
|
|
|
2021-11-28 23:50:27 +01:00
|
|
|
|
return bitmapImage;
|
2019-06-15 01:39:07 +02:00
|
|
|
|
}
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|