2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
4
using System . Linq ;
5
+ using System . Threading ;
5
6
using System . Windows ;
6
7
using System . Windows . Media ;
7
8
using System . Windows . Media . Imaging ;
@@ -16,7 +17,7 @@ public static class WallpaperPathRetrieval
16
17
17
18
private const int MaxCacheSize = 3 ;
18
19
private static readonly Dictionary < ( string , DateTime ) , ImageBrush > WallpaperCache = new ( ) ;
19
- private static readonly object CacheLock = new ( ) ;
20
+ private static readonly Lock CacheLock = new ( ) ;
20
21
21
22
public static Brush GetWallpaperBrush ( )
22
23
{
@@ -31,7 +32,7 @@ public static Brush GetWallpaperBrush()
31
32
var wallpaperPath = Win32Helper . GetWallpaperPath ( ) ;
32
33
if ( string . IsNullOrEmpty ( wallpaperPath ) || ! File . Exists ( wallpaperPath ) )
33
34
{
34
- App . API . LogInfo ( ClassName , $ "Wallpaper path is invalid: { wallpaperPath } ") ;
35
+ App . API . LogError ( ClassName , $ "Wallpaper path is invalid: { wallpaperPath } ") ;
35
36
var wallpaperColor = GetWallpaperColor ( ) ;
36
37
return new SolidColorBrush ( wallpaperColor ) ;
37
38
}
@@ -47,17 +48,22 @@ public static Brush GetWallpaperBrush()
47
48
return cachedWallpaper ;
48
49
}
49
50
}
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
+ }
56
61
57
62
if ( originalWidth == 0 || originalHeight == 0 )
58
63
{
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 ) ;
61
67
}
62
68
63
69
// Calculate the scaling factor to fit the image within 800x600 while preserving aspect ratio
@@ -70,7 +76,9 @@ public static Brush GetWallpaperBrush()
70
76
// Set DecodePixelWidth and DecodePixelHeight to resize the image while preserving aspect ratio
71
77
var bitmap = new BitmapImage ( ) ;
72
78
bitmap . BeginInit ( ) ;
79
+ bitmap . CacheOption = BitmapCacheOption . OnLoad ; // Use OnLoad to ensure the wallpaper file is not locked
73
80
bitmap . UriSource = new Uri ( wallpaperPath ) ;
81
+ bitmap . CreateOptions = BitmapCreateOptions . IgnoreColorProfile ;
74
82
bitmap . DecodePixelWidth = decodedPixelWidth ;
75
83
bitmap . DecodePixelHeight = decodedPixelHeight ;
76
84
bitmap . EndInit ( ) ;
@@ -104,13 +112,13 @@ public static Brush GetWallpaperBrush()
104
112
105
113
private static Color GetWallpaperColor ( )
106
114
{
107
- RegistryKey key = Registry . CurrentUser . OpenSubKey ( @"Control Panel\Colors" , false ) ;
115
+ using var key = Registry . CurrentUser . OpenSubKey ( @"Control Panel\Colors" , false ) ;
108
116
var result = key ? . GetValue ( "Background" , null ) ;
109
117
if ( result is string strResult )
110
118
{
111
119
try
112
120
{
113
- var parts = strResult . Trim ( ) . Split ( new [ ] { ' ' } , 3 ) . Select ( byte . Parse ) . ToList ( ) ;
121
+ var parts = strResult . Trim ( ) . Split ( [ ' ' ] , 3 ) . Select ( byte . Parse ) . ToList ( ) ;
114
122
return Color . FromRgb ( parts [ 0 ] , parts [ 1 ] , parts [ 2 ] ) ;
115
123
}
116
124
catch ( Exception ex )
0 commit comments