Update ImageLoader.cs

This commit is contained in:
ClemensFischer 2025-08-21 21:48:08 +02:00
parent 178cec8e66
commit 6f82f8012a

View file

@ -2,8 +2,8 @@
using System; using System;
using System.IO; using System.IO;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
#if WPF #if WPF
using System.Windows.Media; using System.Windows.Media;
#elif UWP #elif UWP
@ -105,16 +105,7 @@ namespace MapControl
{ {
if (responseMessage.IsSuccessStatusCode) if (responseMessage.IsSuccessStatusCode)
{ {
byte[] buffer; byte[] buffer = await responseMessage.Content.ReadAsByteArrayAsync(progress, cancellationToken).ConfigureAwait(false);
if (progress != null && responseMessage.Content.Headers.ContentLength.HasValue)
{
buffer = await responseMessage.Content.ReadAsByteArrayAsync(progress, cancellationToken).ConfigureAwait(false);
}
else
{
buffer = await responseMessage.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);
}
response = new HttpResponse(buffer, responseMessage.Headers.CacheControl?.MaxAge); response = new HttpResponse(buffer, responseMessage.Headers.CacheControl?.MaxAge);
} }
@ -147,9 +138,13 @@ namespace MapControl
internal static class HttpContentExtensions internal static class HttpContentExtensions
{ {
public static async Task<byte[]> ReadAsByteArrayAsync(this HttpContent content, IProgress<double> progress, CancellationToken cancellationToken) public static async Task<byte[]> ReadAsByteArrayAsync(this HttpContent content, IProgress<double> progress, CancellationToken cancellationToken)
{
byte[] buffer;
if (progress != null && content.Headers.ContentLength.HasValue)
{ {
var length = (int)content.Headers.ContentLength.Value; var length = (int)content.Headers.ContentLength.Value;
var buffer = new byte[length]; buffer = new byte[length];
using (var stream = await content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false)) using (var stream = await content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false))
{ {
@ -169,6 +164,11 @@ namespace MapControl
} }
} }
} }
}
else
{
buffer = await content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);
}
return buffer; return buffer;
} }