From a1f513852ed0fc083b8a8b7546c8129298d098e4 Mon Sep 17 00:00:00 2001 From: Scighost Date: Wed, 17 Jan 2024 10:04:34 +0800 Subject: [PATCH] use image thumbnail to limit memory increase --- src/Starward/Controls/CachedImage.cs | 33 +++++++++++++++++++++++++- src/Starward/Pages/ImageViewPage.xaml | 2 ++ src/Starward/Pages/ScreenshotPage.xaml | 2 ++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Starward/Controls/CachedImage.cs b/src/Starward/Controls/CachedImage.cs index 7a0eb62e7..f3f21296f 100644 --- a/src/Starward/Controls/CachedImage.cs +++ b/src/Starward/Controls/CachedImage.cs @@ -1,4 +1,5 @@ using CommunityToolkit.WinUI.UI.Controls; +using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media.Imaging; using Starward.Services.Cache; @@ -7,6 +8,8 @@ using System.IO; using System.Threading; using System.Threading.Tasks; +using Windows.Storage; +using Windows.Storage.FileProperties; namespace Starward.Controls; @@ -23,14 +26,42 @@ public static void ClearCache() } + + public bool IsThumbnail + { + get { return (bool)GetValue(IsThumbnailProperty); } + set { SetValue(IsThumbnailProperty, value); } + } + + public static readonly DependencyProperty IsThumbnailProperty = + DependencyProperty.Register("IsThumbnail", typeof(bool), typeof(CachedImage), new PropertyMetadata(false)); + + + + protected override async Task ProvideCachedResourceAsync(Uri imageUri, CancellationToken token) { try { - if (imageUri.Scheme is "ms-appx" or "file") + if (imageUri.Scheme is "ms-appx") { return new BitmapImage(imageUri); } + else if (imageUri.Scheme is "file") + { + if (IsThumbnail) + { + StorageFile file = await StorageFile.GetFileFromPathAsync(imageUri.LocalPath); + StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, 240, ThumbnailOptions.UseCurrentScale); + BitmapImage image = new BitmapImage(); + await image.SetSourceAsync(thumbnail); + return image; + } + else + { + return new BitmapImage(imageUri); + } + } else { if (fileCache.TryGetValue(imageUri, out var uri)) diff --git a/src/Starward/Pages/ImageViewPage.xaml b/src/Starward/Pages/ImageViewPage.xaml index 6d57b2f54..f65dad142 100644 --- a/src/Starward/Pages/ImageViewPage.xaml +++ b/src/Starward/Pages/ImageViewPage.xaml @@ -83,6 +83,8 @@ diff --git a/src/Starward/Pages/ScreenshotPage.xaml b/src/Starward/Pages/ScreenshotPage.xaml index a659a155c..0a354192f 100644 --- a/src/Starward/Pages/ScreenshotPage.xaml +++ b/src/Starward/Pages/ScreenshotPage.xaml @@ -147,6 +147,8 @@ ui:VisualExtensions.NormalizedCenterPoint="0.5" Background="{ThemeResource ControlAltFillColorTertiaryBrush}" EnableLazyLoading="True" + IsCacheEnabled="True" + IsThumbnail="True" Source="{x:Bind FullName}" Stretch="UniformToFill">