22using System . Collections . Generic ;
33using System . IO ;
44using System . Linq ;
5+ using System . Threading ;
56using System . Windows ;
67using System . Windows . Media ;
78using System . Windows . Media . Imaging ;
@@ -16,7 +17,7 @@ public static class WallpaperPathRetrieval
1617
1718 private const int MaxCacheSize = 3 ;
1819 private static readonly Dictionary < ( string , DateTime ) , ImageBrush > WallpaperCache = new ( ) ;
19- private static readonly object CacheLock = new ( ) ;
20+ private static readonly Lock CacheLock = new ( ) ;
2021
2122 public static Brush GetWallpaperBrush ( )
2223 {
@@ -31,7 +32,7 @@ public static Brush GetWallpaperBrush()
3132 var wallpaperPath = Win32Helper . GetWallpaperPath ( ) ;
3233 if ( string . IsNullOrEmpty ( wallpaperPath ) || ! File . Exists ( wallpaperPath ) )
3334 {
34- App . API . LogInfo ( ClassName , $ "Wallpaper path is invalid: { wallpaperPath } ") ;
35+ App . API . LogError ( ClassName , $ "Wallpaper path is invalid: { wallpaperPath } ") ;
3536 var wallpaperColor = GetWallpaperColor ( ) ;
3637 return new SolidColorBrush ( wallpaperColor ) ;
3738 }
@@ -47,17 +48,22 @@ public static Brush GetWallpaperBrush()
4748 return cachedWallpaper ;
4849 }
4950 }
50-
51- using var fileStream = File . OpenRead ( wallpaperPath ) ;
52- var decoder = BitmapDecoder . Create ( fileStream , BitmapCreateOptions . DelayCreation , BitmapCacheOption . None ) ;
53- var frame = decoder . Frames [ 0 ] ;
54- var originalWidth = frame . PixelWidth ;
55- var originalHeight = frame . PixelHeight ;
51+
52+ int originalWidth , originalHeight ;
53+ // Use `using ()` instead of `using var` sentence here to ensure the wallpaper file is not locked
54+ using ( var fileStream = File . OpenRead ( wallpaperPath ) )
55+ {
56+ var decoder = BitmapDecoder . Create ( fileStream , BitmapCreateOptions . DelayCreation , BitmapCacheOption . None ) ;
57+ var frame = decoder . Frames [ 0 ] ;
58+ originalWidth = frame . PixelWidth ;
59+ originalHeight = frame . PixelHeight ;
60+ }
5661
5762 if ( originalWidth == 0 || originalHeight == 0 )
5863 {
59- App . API . LogInfo ( ClassName , $ "Failed to load bitmap: Width={ originalWidth } , Height={ originalHeight } ") ;
60- return new SolidColorBrush ( Colors . Transparent ) ;
64+ App . API . LogError ( ClassName , $ "Failed to load bitmap: Width={ originalWidth } , Height={ originalHeight } ") ;
65+ var wallpaperColor = GetWallpaperColor ( ) ;
66+ return new SolidColorBrush ( wallpaperColor ) ;
6167 }
6268
6369 // Calculate the scaling factor to fit the image within 800x600 while preserving aspect ratio
@@ -70,7 +76,9 @@ public static Brush GetWallpaperBrush()
7076 // Set DecodePixelWidth and DecodePixelHeight to resize the image while preserving aspect ratio
7177 var bitmap = new BitmapImage ( ) ;
7278 bitmap . BeginInit ( ) ;
79+ bitmap . CacheOption = BitmapCacheOption . OnLoad ; // Use OnLoad to ensure the wallpaper file is not locked
7380 bitmap . UriSource = new Uri ( wallpaperPath ) ;
81+ bitmap . CreateOptions = BitmapCreateOptions . IgnoreColorProfile ;
7482 bitmap . DecodePixelWidth = decodedPixelWidth ;
7583 bitmap . DecodePixelHeight = decodedPixelHeight ;
7684 bitmap . EndInit ( ) ;
@@ -104,13 +112,13 @@ public static Brush GetWallpaperBrush()
104112
105113 private static Color GetWallpaperColor ( )
106114 {
107- RegistryKey key = Registry . CurrentUser . OpenSubKey ( @"Control Panel\Colors" , false ) ;
115+ using var key = Registry . CurrentUser . OpenSubKey ( @"Control Panel\Colors" , false ) ;
108116 var result = key ? . GetValue ( "Background" , null ) ;
109117 if ( result is string strResult )
110118 {
111119 try
112120 {
113- var parts = strResult . Trim ( ) . Split ( new [ ] { ' ' } , 3 ) . Select ( byte . Parse ) . ToList ( ) ;
121+ var parts = strResult . Trim ( ) . Split ( [ ' ' ] , 3 ) . Select ( byte . Parse ) . ToList ( ) ;
114122 return Color . FromRgb ( parts [ 0 ] , parts [ 1 ] , parts [ 2 ] ) ;
115123 }
116124 catch ( Exception ex )
0 commit comments