@@ -17,6 +17,7 @@ public static class WallpaperPathRetrieval
17
17
private static readonly int MAX_PATH = 260 ;
18
18
19
19
private static readonly Dictionary < ( string , DateTime ) , ImageBrush > wallpaperCache = new ( ) ;
20
+ private const int MaxCacheSize = 3 ;
20
21
21
22
public static Brush GetWallpaperBrush ( )
22
23
{
@@ -26,35 +27,55 @@ public static Brush GetWallpaperBrush()
26
27
return Application . Current . Dispatcher . Invoke ( GetWallpaperBrush ) ;
27
28
}
28
29
29
- var wallpaperPath = GetWallpaperPath ( ) ;
30
- if ( wallpaperPath is not null && File . Exists ( wallpaperPath ) )
30
+ try
31
31
{
32
- // Since the wallpaper file name can be the same (TranscodedWallpaper),
33
- // we need to add the last modified date to differentiate them
34
- var dateModified = File . GetLastWriteTime ( wallpaperPath ) ;
35
- wallpaperCache . TryGetValue ( ( wallpaperPath , dateModified ) , out var cachedWallpaper ) ;
36
- if ( cachedWallpaper != null )
32
+ var wallpaperPath = GetWallpaperPath ( ) ;
33
+ if ( wallpaperPath is not null && File . Exists ( wallpaperPath ) )
37
34
{
38
- return cachedWallpaper ;
35
+ // Since the wallpaper file name can be the same (TranscodedWallpaper),
36
+ // we need to add the last modified date to differentiate them
37
+ var dateModified = File . GetLastWriteTime ( wallpaperPath ) ;
38
+ wallpaperCache . TryGetValue ( ( wallpaperPath , dateModified ) , out var cachedWallpaper ) ;
39
+ if ( cachedWallpaper != null )
40
+ {
41
+ return cachedWallpaper ;
42
+ }
43
+
44
+ // We should not dispose the memory stream since the bitmap is still in use
45
+ var memStream = new MemoryStream ( File . ReadAllBytes ( wallpaperPath ) ) ;
46
+ var bitmap = new BitmapImage ( ) ;
47
+ bitmap . BeginInit ( ) ;
48
+ bitmap . StreamSource = memStream ;
49
+ bitmap . DecodePixelWidth = 800 ;
50
+ bitmap . DecodePixelHeight = 600 ;
51
+ bitmap . EndInit ( ) ;
52
+ bitmap . Freeze ( ) ; // Make the bitmap thread-safe
53
+ var wallpaperBrush = new ImageBrush ( bitmap ) { Stretch = Stretch . UniformToFill } ;
54
+ wallpaperBrush . Freeze ( ) ; // Make the brush thread-safe
55
+
56
+ // Manage cache size
57
+ if ( wallpaperCache . Count >= MaxCacheSize )
58
+ {
59
+ // Remove the oldest wallpaper from the cache
60
+ var oldestCache = wallpaperCache . Keys . OrderBy ( k => k . Item2 ) . FirstOrDefault ( ) ;
61
+ if ( oldestCache != default )
62
+ {
63
+ wallpaperCache . Remove ( oldestCache ) ;
64
+ }
65
+ }
66
+
67
+ wallpaperCache . Add ( ( wallpaperPath , dateModified ) , wallpaperBrush ) ;
68
+ return wallpaperBrush ;
39
69
}
40
70
41
- // We should not dispose the memory stream since the bitmap is still in use
42
- var memStream = new MemoryStream ( File . ReadAllBytes ( wallpaperPath ) ) ;
43
- var bitmap = new BitmapImage ( ) ;
44
- bitmap . BeginInit ( ) ;
45
- bitmap . StreamSource = memStream ;
46
- bitmap . DecodePixelWidth = 800 ;
47
- bitmap . DecodePixelHeight = 600 ;
48
- bitmap . EndInit ( ) ;
49
- bitmap . Freeze ( ) ; // Make the bitmap thread-safe
50
- var wallpaperBrush = new ImageBrush ( bitmap ) { Stretch = Stretch . UniformToFill } ;
51
- wallpaperBrush . Freeze ( ) ; // Make the brush thread-safe
52
- wallpaperCache . Add ( ( wallpaperPath , dateModified ) , wallpaperBrush ) ;
53
- return wallpaperBrush ;
71
+ var wallpaperColor = GetWallpaperColor ( ) ;
72
+ return new SolidColorBrush ( wallpaperColor ) ;
73
+ }
74
+ catch ( Exception ex )
75
+ {
76
+ App . API . LogException ( nameof ( WallpaperPathRetrieval ) , "Error retrieving wallpaper" , ex ) ;
77
+ return new SolidColorBrush ( Colors . Transparent ) ;
54
78
}
55
-
56
- var wallpaperColor = GetWallpaperColor ( ) ;
57
- return new SolidColorBrush ( wallpaperColor ) ;
58
79
}
59
80
60
81
private static unsafe string GetWallpaperPath ( )
0 commit comments