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

60 lines
1.5 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2022-01-14 20:22:56 +01:00
// © 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.IO;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MapControl
{
public static partial class ImageLoader
{
2021-11-28 23:50:27 +01:00
public static Task<ImageSource> LoadImageAsync(Stream stream)
{
2021-11-28 23:50:27 +01:00
return Task.Run(() => LoadImage(stream));
}
2021-11-28 23:50:27 +01:00
public static Task<ImageSource> LoadImageAsync(byte[] buffer)
{
2021-11-28 23:50:27 +01:00
return Task.Run(() =>
{
2021-11-28 23:50:27 +01:00
using (var stream = new MemoryStream(buffer))
{
return LoadImage(stream);
}
});
}
2021-11-28 23:50:27 +01:00
public static Task<ImageSource> LoadImageAsync(string path)
{
2021-11-28 23:50:27 +01:00
return Task.Run(() =>
{
2021-11-28 23:50:27 +01:00
if (!File.Exists(path))
{
2021-11-28 23:50:27 +01:00
return null;
}
2021-11-28 23:50:27 +01:00
using (var stream = File.OpenRead(path))
{
return LoadImage(stream);
}
});
}
2021-11-28 23:50:27 +01:00
private static ImageSource LoadImage(Stream stream)
{
2021-11-28 23:50:27 +01:00
var bitmapImage = new BitmapImage();
2021-11-28 23:50:27 +01:00
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
bitmapImage.Freeze();
2021-11-28 23:50:27 +01:00
return bitmapImage;
}
}
}