mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-04 14:08:32 +00:00
Avalonia ImageLoader
This commit is contained in:
parent
3cc93c7460
commit
4867d52013
5 changed files with 69 additions and 36 deletions
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Media.Imaging;
|
||||
using Avalonia.Platform;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
|
@ -29,22 +30,57 @@ namespace MapControl
|
|||
|
||||
public static Task<IImage> LoadImageAsync(string path)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
return Task.FromResult<IImage>(null);
|
||||
}
|
||||
|
||||
return Task.Run(() =>
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var stream = File.OpenRead(path);
|
||||
|
||||
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>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>disable</Nullable>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<RootNamespace>MapControl</RootNamespace>
|
||||
<AssemblyTitle>XAML Map Control Library for Avalonia UI</AssemblyTitle>
|
||||
<Product>XAML Map Control</Product>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue