mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-03 03:57:14 +02:00
Avalonia ImageLoader
This commit is contained in:
parent
3cc93c7460
commit
4867d52013
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
using Avalonia.Media;
|
using Avalonia.Media;
|
||||||
using Avalonia.Media.Imaging;
|
using Avalonia.Media.Imaging;
|
||||||
|
using Avalonia.Platform;
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
@ -29,22 +30,57 @@ namespace MapControl
|
||||||
|
|
||||||
public static Task<IImage> LoadImageAsync(string path)
|
public static Task<IImage> LoadImageAsync(string path)
|
||||||
{
|
{
|
||||||
|
if (!File.Exists(path))
|
||||||
|
{
|
||||||
|
return Task.FromResult<IImage>(null);
|
||||||
|
}
|
||||||
|
|
||||||
return Task.Run(() =>
|
return Task.Run(() =>
|
||||||
{
|
{
|
||||||
if (!File.Exists(path))
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
using var stream = File.OpenRead(path);
|
using var stream = File.OpenRead(path);
|
||||||
|
|
||||||
return LoadImage(stream);
|
return LoadImage(stream);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static Task<IImage> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
internal static async Task<IImage> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
||||||
{
|
{
|
||||||
return Task.FromResult<IImage>(null);
|
var images = await LoadImagesAsync(uri1, uri2, progress);
|
||||||
|
|
||||||
|
WriteableBitmap image = null;
|
||||||
|
|
||||||
|
if (images.Length == 2 &&
|
||||||
|
images[0] is Bitmap image1 &&
|
||||||
|
images[1] is Bitmap image2 &&
|
||||||
|
image1.PixelSize.Height == image2.PixelSize.Height &&
|
||||||
|
image1.Format.HasValue &&
|
||||||
|
image2.Format.HasValue &&
|
||||||
|
image1.Format.Value == image2.Format.Value &&
|
||||||
|
image1.AlphaFormat.HasValue &&
|
||||||
|
image2.AlphaFormat.HasValue &&
|
||||||
|
image1.AlphaFormat.Value == image2.AlphaFormat.Value)
|
||||||
|
{
|
||||||
|
var bpp = image1.Format.Value == PixelFormat.Rgb565 ? 2 : 4;
|
||||||
|
var size = new PixelSize(image1.PixelSize.Width + image2.PixelSize.Width, image1.PixelSize.Height);
|
||||||
|
var stride1 = image1.PixelSize.Width * bpp;
|
||||||
|
var stride = size.Width * bpp;
|
||||||
|
var buffer = new byte[stride * size.Height];
|
||||||
|
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
fixed (byte* ptr = buffer)
|
||||||
|
{
|
||||||
|
var p = (nint)ptr;
|
||||||
|
|
||||||
|
image1.CopyPixels(new PixelRect(image1.PixelSize), p, buffer.Length, stride);
|
||||||
|
image2.CopyPixels(new PixelRect(image2.PixelSize), p + stride1, buffer.Length, stride);
|
||||||
|
|
||||||
|
image = new WriteableBitmap(image1.Format.Value, image1.AlphaFormat.Value, p, size, image1.Dpi, stride);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return image;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<Nullable>disable</Nullable>
|
<Nullable>disable</Nullable>
|
||||||
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
<RootNamespace>MapControl</RootNamespace>
|
<RootNamespace>MapControl</RootNamespace>
|
||||||
<AssemblyTitle>XAML Map Control Library for Avalonia UI</AssemblyTitle>
|
<AssemblyTitle>XAML Map Control Library for Avalonia UI</AssemblyTitle>
|
||||||
<Product>XAML Map Control</Product>
|
<Product>XAML Map Control</Product>
|
||||||
|
|
|
||||||
|
|
@ -159,5 +159,21 @@ namespace MapControl
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Task<ImageSource[]> LoadImagesAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
||||||
|
{
|
||||||
|
IProgress<double> progress1 = null;
|
||||||
|
IProgress<double> progress2 = null;
|
||||||
|
|
||||||
|
if (progress != null)
|
||||||
|
{
|
||||||
|
var p1 = 0d;
|
||||||
|
var p2 = 0d;
|
||||||
|
progress1 = new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); });
|
||||||
|
progress2 = new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); });
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.WhenAll(LoadImageAsync(uri1, progress1), LoadImageAsync(uri2, progress2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -38,13 +38,13 @@ namespace MapControl
|
||||||
|
|
||||||
public static Task<ImageSource> LoadImageAsync(string path)
|
public static Task<ImageSource> LoadImageAsync(string path)
|
||||||
{
|
{
|
||||||
|
if (!File.Exists(path))
|
||||||
|
{
|
||||||
|
return Task.FromResult<ImageSource>(null);
|
||||||
|
}
|
||||||
|
|
||||||
return Task.Run(() =>
|
return Task.Run(() =>
|
||||||
{
|
{
|
||||||
if (!File.Exists(path))
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
using (var stream = File.OpenRead(path))
|
using (var stream = File.OpenRead(path))
|
||||||
{
|
{
|
||||||
return LoadImage(stream);
|
return LoadImage(stream);
|
||||||
|
|
@ -54,19 +54,9 @@ namespace MapControl
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
var images = await LoadImagesAsync(uri1, uri2, progress);
|
||||||
|
|
||||||
WriteableBitmap image = null;
|
WriteableBitmap image = null;
|
||||||
IProgress<double> progress1 = null;
|
|
||||||
IProgress<double> progress2 = null;
|
|
||||||
|
|
||||||
if (progress != null)
|
|
||||||
{
|
|
||||||
var p1 = 0d;
|
|
||||||
var p2 = 0d;
|
|
||||||
progress1 = new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); });
|
|
||||||
progress2 = new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); });
|
|
||||||
}
|
|
||||||
|
|
||||||
var images = await Task.WhenAll(LoadImageAsync(uri1, progress1), LoadImageAsync(uri2, progress2));
|
|
||||||
|
|
||||||
if (images.Length == 2 &&
|
if (images.Length == 2 &&
|
||||||
images[0] is BitmapSource image1 &&
|
images[0] is BitmapSource image1 &&
|
||||||
|
|
|
||||||
|
|
@ -77,19 +77,9 @@ namespace MapControl
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
var images = await LoadImagesAsync(uri1, uri2, progress);
|
||||||
|
|
||||||
WriteableBitmap image = null;
|
WriteableBitmap image = null;
|
||||||
IProgress<double> progress1 = null;
|
|
||||||
IProgress<double> progress2 = null;
|
|
||||||
|
|
||||||
if (progress != null)
|
|
||||||
{
|
|
||||||
var p1 = 0d;
|
|
||||||
var p2 = 0d;
|
|
||||||
progress1 = new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); });
|
|
||||||
progress2 = new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); });
|
|
||||||
}
|
|
||||||
|
|
||||||
var images = await Task.WhenAll(LoadImageAsync(uri1, progress1), LoadImageAsync(uri2, progress2));
|
|
||||||
|
|
||||||
if (images.Length == 2 &&
|
if (images.Length == 2 &&
|
||||||
images[0] is WriteableBitmap image1 &&
|
images[0] is WriteableBitmap image1 &&
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue