2025-03-31 21:33:52 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using System;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
using System.IO;
|
2022-11-30 17:59:38 +01:00
|
|
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
2025-08-19 23:20:11 +02:00
|
|
|
|
using System.Threading;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2022-11-30 17:59:38 +01:00
|
|
|
|
using Windows.Graphics.Imaging;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
using Windows.Storage;
|
|
|
|
|
|
using Windows.Storage.Streams;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if UWP
|
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;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#else
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media.Imaging;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
#endif
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2018-08-08 23:31:52 +02:00
|
|
|
|
public static partial class ImageLoader
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2024-05-19 17:24:18 +02:00
|
|
|
|
public static ImageSource LoadImage(Uri uri)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new BitmapImage(uri);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-17 23:29:41 +02:00
|
|
|
|
public static async Task<ImageSource> LoadImageAsync(IRandomAccessStream stream)
|
|
|
|
|
|
{
|
2025-01-01 18:20:33 +01:00
|
|
|
|
var image = new BitmapImage();
|
|
|
|
|
|
await image.SetSourceAsync(stream);
|
|
|
|
|
|
return image;
|
2023-08-17 23:29:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-02 11:35:20 +02:00
|
|
|
|
public static Task<ImageSource> LoadImageAsync(Stream stream)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2021-07-02 11:35:20 +02:00
|
|
|
|
return LoadImageAsync(stream.AsRandomAccessStream());
|
|
|
|
|
|
}
|
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
|
|
|
|
|
2022-12-06 18:07:38 +01:00
|
|
|
|
path = FilePath.GetFullPath(path);
|
2022-12-05 23:34:03 +01:00
|
|
|
|
|
2018-08-15 23:29:03 +02:00
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
|
{
|
2022-12-05 23:34:03 +01:00
|
|
|
|
var file = await StorageFile.GetFileFromPathAsync(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
|
|
|
|
}
|
2022-11-30 17:59:38 +01:00
|
|
|
|
|
2025-01-01 18:20:33 +01:00
|
|
|
|
internal static async Task<WriteableBitmap> LoadWriteableBitmapAsync(BitmapDecoder decoder)
|
2022-11-30 17:59:38 +01:00
|
|
|
|
{
|
2025-01-01 18:20:33 +01:00
|
|
|
|
var image = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
|
|
|
|
|
|
var pixelData = await decoder.GetPixelDataAsync(
|
|
|
|
|
|
BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(),
|
|
|
|
|
|
ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage);
|
|
|
|
|
|
|
|
|
|
|
|
pixelData.DetachPixelData().CopyTo(image.PixelBuffer);
|
|
|
|
|
|
|
|
|
|
|
|
return image;
|
|
|
|
|
|
}
|
2022-11-30 17:59:38 +01:00
|
|
|
|
|
2025-08-19 23:20:11 +02:00
|
|
|
|
internal static async Task<WriteableBitmap> LoadWriteableBitmapAsync(Uri uri, IProgress<double> progress, CancellationToken cancellationToken)
|
2025-01-01 18:20:33 +01:00
|
|
|
|
{
|
2025-01-01 19:45:40 +01:00
|
|
|
|
WriteableBitmap bitmap = null;
|
2022-11-30 17:59:38 +01:00
|
|
|
|
|
2025-01-02 13:44:46 +01:00
|
|
|
|
progress.Report(0d);
|
|
|
|
|
|
|
2025-01-01 18:20:33 +01:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-08-19 23:20:11 +02:00
|
|
|
|
var response = await GetHttpResponseAsync(uri, progress, cancellationToken);
|
2025-01-02 13:44:46 +01:00
|
|
|
|
|
|
|
|
|
|
if (response?.Buffer != null)
|
2025-01-01 18:20:33 +01:00
|
|
|
|
{
|
2025-01-02 13:44:46 +01:00
|
|
|
|
using (var memoryStream = new MemoryStream(response.Buffer))
|
|
|
|
|
|
using (var randomAccessStream = memoryStream.AsRandomAccessStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
var decoder = await BitmapDecoder.CreateAsync(randomAccessStream);
|
2025-01-01 19:45:40 +01:00
|
|
|
|
|
2025-01-02 13:44:46 +01:00
|
|
|
|
bitmap = await LoadWriteableBitmapAsync(decoder);
|
|
|
|
|
|
}
|
2025-01-01 18:20:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-03-31 21:33:52 +02:00
|
|
|
|
Logger?.LogError(ex, "{uri}", uri);
|
2025-01-01 18:20:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-02 13:44:46 +01:00
|
|
|
|
progress.Report(1d);
|
|
|
|
|
|
|
2025-01-01 19:45:40 +01:00
|
|
|
|
return bitmap;
|
2025-01-01 18:20:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-19 23:20:11 +02:00
|
|
|
|
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress, CancellationToken cancellationToken)
|
2025-01-01 18:20:33 +01:00
|
|
|
|
{
|
2025-01-01 19:45:40 +01:00
|
|
|
|
WriteableBitmap mergedBitmap = null;
|
2025-01-02 13:44:46 +01:00
|
|
|
|
var p1 = 0d;
|
|
|
|
|
|
var p2 = 0d;
|
2025-01-01 18:20:33 +01:00
|
|
|
|
|
2025-01-02 13:44:46 +01:00
|
|
|
|
var bitmaps = await Task.WhenAll(
|
2025-08-19 23:20:11 +02:00
|
|
|
|
LoadWriteableBitmapAsync(uri1, new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); }), cancellationToken),
|
|
|
|
|
|
LoadWriteableBitmapAsync(uri2, new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); }), cancellationToken));
|
2025-01-01 18:20:33 +01:00
|
|
|
|
|
2025-08-19 23:20:11 +02:00
|
|
|
|
if (!cancellationToken.IsCancellationRequested &&
|
|
|
|
|
|
bitmaps.Length == 2 &&
|
2025-01-01 19:45:40 +01:00
|
|
|
|
bitmaps[0] != null &&
|
|
|
|
|
|
bitmaps[1] != null &&
|
|
|
|
|
|
bitmaps[0].PixelHeight == bitmaps[1].PixelHeight)
|
2022-11-30 17:59:38 +01:00
|
|
|
|
{
|
2025-01-01 19:45:40 +01:00
|
|
|
|
var buffer1 = bitmaps[0].PixelBuffer;
|
|
|
|
|
|
var buffer2 = bitmaps[1].PixelBuffer;
|
|
|
|
|
|
var stride1 = (uint)bitmaps[0].PixelWidth * 4;
|
|
|
|
|
|
var stride2 = (uint)bitmaps[1].PixelWidth * 4;
|
2023-08-18 09:07:18 +02:00
|
|
|
|
var stride = stride1 + stride2;
|
2025-01-01 19:45:40 +01:00
|
|
|
|
var height = bitmaps[0].PixelHeight;
|
2022-11-30 17:59:38 +01:00
|
|
|
|
|
2025-01-01 19:45:40 +01:00
|
|
|
|
mergedBitmap = new WriteableBitmap(bitmaps[0].PixelWidth + bitmaps[1].PixelWidth, height);
|
2022-11-30 17:59:38 +01:00
|
|
|
|
|
2025-01-01 19:45:40 +01:00
|
|
|
|
var buffer = mergedBitmap.PixelBuffer;
|
2023-08-18 09:07:18 +02:00
|
|
|
|
|
|
|
|
|
|
for (uint y = 0; y < height; y++)
|
2022-11-30 17:59:38 +01:00
|
|
|
|
{
|
2023-08-18 09:07:18 +02:00
|
|
|
|
buffer1.CopyTo(y * stride1, buffer, y * stride, stride1);
|
|
|
|
|
|
buffer2.CopyTo(y * stride2, buffer, y * stride + stride1, stride2);
|
2022-11-30 17:59:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-01 19:45:40 +01:00
|
|
|
|
return mergedBitmap;
|
2022-11-30 17:59:38 +01:00
|
|
|
|
}
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|