11using System ;
22using System . Collections . Concurrent ;
33using System . Collections . Generic ;
4+ using System . Diagnostics ;
45using System . IO ;
56using System . Linq ;
67using System . Net ;
@@ -21,8 +22,10 @@ public static class ImageLoader
2122 private static readonly ConcurrentDictionary < string , string > GuidToKey = new ( ) ;
2223 private static IImageHashGenerator _hashGenerator ;
2324 private static readonly bool EnableImageHash = true ;
24- public static ImageSource DefaultImage { get ; } = new BitmapImage ( new Uri ( Constant . MissingImgIcon ) ) ;
25- public const int SmallIconSize = 32 ;
25+ public static ImageSource MissingImage { get ; } = new BitmapImage ( new Uri ( Constant . MissingImgIcon ) ) ;
26+ public static ImageSource LoadingImage { get ; } = new BitmapImage ( new Uri ( Constant . LoadingImgIcon ) ) ;
27+ public const int SmallIconSize = 64 ;
28+ public const int FullIconSize = 256 ;
2629
2730
2831 private static readonly string [ ] ImageExtensions =
@@ -99,6 +102,7 @@ private enum ImageType
99102 Folder ,
100103 Data ,
101104 ImageFile ,
105+ FullImageFile ,
102106 Error ,
103107 Cache
104108 }
@@ -111,7 +115,7 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
111115 {
112116 if ( string . IsNullOrEmpty ( path ) )
113117 {
114- return new ImageResult ( DefaultImage , ImageType . Error ) ;
118+ return new ImageResult ( MissingImage , ImageType . Error ) ;
115119 }
116120
117121 if ( ImageCache . ContainsKey ( path , loadFullImage ) )
@@ -201,6 +205,7 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
201205 if ( loadFullImage )
202206 {
203207 image = LoadFullImage ( path ) ;
208+ type = ImageType . FullImageFile ;
204209 }
205210 else
206211 {
@@ -215,7 +220,7 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
215220 else
216221 {
217222 type = ImageType . File ;
218- image = GetThumbnail ( path , ThumbnailOptions . None ) ;
223+ image = GetThumbnail ( path , ThumbnailOptions . None , loadFullImage ? FullIconSize : SmallIconSize ) ;
219224 }
220225 }
221226 else
@@ -232,12 +237,12 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
232237 return new ImageResult ( image , type ) ;
233238 }
234239
235- private static BitmapSource GetThumbnail ( string path , ThumbnailOptions option = ThumbnailOptions . ThumbnailOnly )
240+ private static BitmapSource GetThumbnail ( string path , ThumbnailOptions option = ThumbnailOptions . ThumbnailOnly , int size = SmallIconSize )
236241 {
237242 return WindowsThumbnailProvider . GetThumbnail (
238243 path ,
239- Constant . ThumbnailSize ,
240- Constant . ThumbnailSize ,
244+ size ,
245+ size ,
241246 option ) ;
242247 }
243248
@@ -254,6 +259,10 @@ public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullI
254259 if ( imageResult . ImageType != ImageType . Error && imageResult . ImageType != ImageType . Cache )
255260 { // we need to get image hash
256261 string hash = EnableImageHash ? _hashGenerator . GetHashFromImage ( img ) : null ;
262+ if ( imageResult . ImageType == ImageType . FullImageFile )
263+ {
264+ path = $ "{ path } _{ ImageType . FullImageFile } ";
265+ }
257266 if ( hash != null )
258267 {
259268
@@ -263,6 +272,7 @@ public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullI
263272 }
264273 else
265274 { // new guid
275+
266276 GuidToKey [ hash ] = path ;
267277 }
268278 }
@@ -279,9 +289,33 @@ private static BitmapImage LoadFullImage(string path)
279289 BitmapImage image = new BitmapImage ( ) ;
280290 image . BeginInit ( ) ;
281291 image . CacheOption = BitmapCacheOption . OnLoad ;
282- image . UriSource = new Uri ( path ) ;
292+ image . UriSource = new Uri ( path ) ;
283293 image . CreateOptions = BitmapCreateOptions . IgnoreColorProfile ;
284294 image . EndInit ( ) ;
295+
296+ if ( image . PixelWidth > 320 )
297+ {
298+ BitmapImage resizedWidth = new BitmapImage ( ) ;
299+ resizedWidth . BeginInit ( ) ;
300+ resizedWidth . CacheOption = BitmapCacheOption . OnLoad ;
301+ resizedWidth . UriSource = new Uri ( path ) ;
302+ resizedWidth . CreateOptions = BitmapCreateOptions . IgnoreColorProfile ;
303+ resizedWidth . DecodePixelWidth = 320 ;
304+ resizedWidth . EndInit ( ) ;
305+
306+ if ( resizedWidth . PixelHeight > 320 )
307+ {
308+ BitmapImage resizedHeight = new BitmapImage ( ) ;
309+ resizedHeight . BeginInit ( ) ;
310+ resizedHeight . CacheOption = BitmapCacheOption . OnLoad ;
311+ resizedHeight . UriSource = new Uri ( path ) ;
312+ resizedHeight . CreateOptions = BitmapCreateOptions . IgnoreColorProfile ;
313+ resizedHeight . DecodePixelHeight = 320 ;
314+ resizedHeight . EndInit ( ) ;
315+ return resizedHeight ;
316+ }
317+ return resizedWidth ;
318+ }
285319 return image ;
286320 }
287321 }
0 commit comments