Image loading with cancellation

This commit is contained in:
ClemensFischer 2025-08-19 23:20:11 +02:00
parent 81eabef257
commit 69bba213f0
13 changed files with 103 additions and 88 deletions

View file

@ -2,6 +2,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Storage;
@ -66,7 +67,7 @@ namespace MapControl
return image;
}
internal static async Task<WriteableBitmap> LoadWriteableBitmapAsync(Uri uri, IProgress<double> progress)
internal static async Task<WriteableBitmap> LoadWriteableBitmapAsync(Uri uri, IProgress<double> progress, CancellationToken cancellationToken)
{
WriteableBitmap bitmap = null;
@ -74,7 +75,7 @@ namespace MapControl
try
{
var response = await GetHttpResponseAsync(uri, progress);
var response = await GetHttpResponseAsync(uri, progress, cancellationToken);
if (response?.Buffer != null)
{
@ -97,17 +98,18 @@ namespace MapControl
return bitmap;
}
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress, CancellationToken cancellationToken)
{
WriteableBitmap mergedBitmap = null;
var p1 = 0d;
var p2 = 0d;
var bitmaps = await Task.WhenAll(
LoadWriteableBitmapAsync(uri1, new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); })),
LoadWriteableBitmapAsync(uri2, new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); })));
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));
if (bitmaps.Length == 2 &&
if (!cancellationToken.IsCancellationRequested &&
bitmaps.Length == 2 &&
bitmaps[0] != null &&
bitmaps[1] != null &&
bitmaps[0].PixelHeight == bitmaps[1].PixelHeight)