From 00c1125124713b2bbcb945bd75e433d4455197da Mon Sep 17 00:00:00 2001 From: Neakita Date: Wed, 23 Oct 2024 22:32:41 +0500 Subject: [PATCH] Adaptive annotation Thumbnail scaling --- .../Annotation/ScreenshotViewModel.cs | 2 +- .../Annotation/Screenshots.axaml | 22 ++++++++---- .../ScreenshotToScaledBitmapConverter.cs | 36 +++++++++++++++++++ 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 SightKeeper.Avalonia/ScreenshotToScaledBitmapConverter.cs diff --git a/SightKeeper.Avalonia/Annotation/ScreenshotViewModel.cs b/SightKeeper.Avalonia/Annotation/ScreenshotViewModel.cs index 4a8c18a7..18253352 100644 --- a/SightKeeper.Avalonia/Annotation/ScreenshotViewModel.cs +++ b/SightKeeper.Avalonia/Annotation/ScreenshotViewModel.cs @@ -43,7 +43,7 @@ private unsafe WriteableBitmap LoadImage() return bitmap; } - private unsafe WriteableBitmap LoadImage(int maximumLargestDimension) + internal unsafe WriteableBitmap LoadImage(int maximumLargestDimension) { PixelSize size = ComputeThumbnailSize(maximumLargestDimension); WriteableBitmap bitmap = _bitmapPool.Rent(size, PixelFormat.Rgb32); diff --git a/SightKeeper.Avalonia/Annotation/Screenshots.axaml b/SightKeeper.Avalonia/Annotation/Screenshots.axaml index 2629342e..b923e90e 100644 --- a/SightKeeper.Avalonia/Annotation/Screenshots.axaml +++ b/SightKeeper.Avalonia/Annotation/Screenshots.axaml @@ -4,6 +4,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:SightKeeper.Avalonia.Annotation" xmlns:behaviors="clr-namespace:SightKeeper.Avalonia.Behaviors" + xmlns:instance="clr-namespace:SightKeeper.Avalonia" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="SightKeeper.Avalonia.Annotation.Screenshots" x:DataType="local:ScreenshotsViewModel"> @@ -18,13 +19,20 @@ - - - - - + + + + + + + + + + + + + diff --git a/SightKeeper.Avalonia/ScreenshotToScaledBitmapConverter.cs b/SightKeeper.Avalonia/ScreenshotToScaledBitmapConverter.cs new file mode 100644 index 00000000..e85eee96 --- /dev/null +++ b/SightKeeper.Avalonia/ScreenshotToScaledBitmapConverter.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using Avalonia; +using Avalonia.Data.Converters; +using SightKeeper.Avalonia.Annotation; + +namespace SightKeeper.Avalonia; + +internal sealed class ScreenshotToScaledBitmapConverter : IMultiValueConverter +{ + private const int SizeStep = 20; + private const int MinimumSize = 20; + + public static ScreenshotToScaledBitmapConverter Instance { get; } = new(); + + public object Convert(IList values, Type targetType, object? parameter, CultureInfo culture) + { + if (values[0] is not ScreenshotViewModel screenshot || + values[1] is not Size targetSize || + targetSize.Width == 0 || + targetSize.Height == 0) + return AvaloniaProperty.UnsetValue; + var largestSizeDimension = Math.Max(targetSize.Width, targetSize.Height); + var roundedLargestSizeDimension = RoundSize(largestSizeDimension); + return screenshot.LoadImage(roundedLargestSizeDimension); + } + + private static int RoundSize(double size) + { + var rounded = (int)Math.Round(size / SizeStep) * SizeStep; + if (rounded < MinimumSize) + return MinimumSize; + return rounded; + } +} \ No newline at end of file