1
1
using System ;
2
2
using System . Collections . Concurrent ;
3
3
using System . Collections . Generic ;
4
+ using System . Diagnostics ;
4
5
using System . IO ;
5
6
using System . Linq ;
6
7
using System . Net ;
@@ -21,8 +22,10 @@ public static class ImageLoader
21
22
private static readonly ConcurrentDictionary < string , string > GuidToKey = new ( ) ;
22
23
private static IImageHashGenerator _hashGenerator ;
23
24
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 ;
26
29
27
30
28
31
private static readonly string [ ] ImageExtensions =
@@ -99,6 +102,7 @@ private enum ImageType
99
102
Folder ,
100
103
Data ,
101
104
ImageFile ,
105
+ FullImageFile ,
102
106
Error ,
103
107
Cache
104
108
}
@@ -111,7 +115,7 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
111
115
{
112
116
if ( string . IsNullOrEmpty ( path ) )
113
117
{
114
- return new ImageResult ( DefaultImage , ImageType . Error ) ;
118
+ return new ImageResult ( MissingImage , ImageType . Error ) ;
115
119
}
116
120
117
121
if ( ImageCache . ContainsKey ( path , loadFullImage ) )
@@ -201,6 +205,7 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
201
205
if ( loadFullImage )
202
206
{
203
207
image = LoadFullImage ( path ) ;
208
+ type = ImageType . FullImageFile ;
204
209
}
205
210
else
206
211
{
@@ -215,7 +220,7 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
215
220
else
216
221
{
217
222
type = ImageType . File ;
218
- image = GetThumbnail ( path , ThumbnailOptions . None ) ;
223
+ image = GetThumbnail ( path , ThumbnailOptions . None , loadFullImage ? FullIconSize : SmallIconSize ) ;
219
224
}
220
225
}
221
226
else
@@ -232,12 +237,12 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
232
237
return new ImageResult ( image , type ) ;
233
238
}
234
239
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 )
236
241
{
237
242
return WindowsThumbnailProvider . GetThumbnail (
238
243
path ,
239
- Constant . ThumbnailSize ,
240
- Constant . ThumbnailSize ,
244
+ size ,
245
+ size ,
241
246
option ) ;
242
247
}
243
248
@@ -254,6 +259,10 @@ public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullI
254
259
if ( imageResult . ImageType != ImageType . Error && imageResult . ImageType != ImageType . Cache )
255
260
{ // we need to get image hash
256
261
string hash = EnableImageHash ? _hashGenerator . GetHashFromImage ( img ) : null ;
262
+ if ( imageResult . ImageType == ImageType . FullImageFile )
263
+ {
264
+ path = $ "{ path } _{ ImageType . FullImageFile } ";
265
+ }
257
266
if ( hash != null )
258
267
{
259
268
@@ -263,6 +272,7 @@ public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullI
263
272
}
264
273
else
265
274
{ // new guid
275
+
266
276
GuidToKey [ hash ] = path ;
267
277
}
268
278
}
@@ -279,9 +289,33 @@ private static BitmapImage LoadFullImage(string path)
279
289
BitmapImage image = new BitmapImage ( ) ;
280
290
image . BeginInit ( ) ;
281
291
image . CacheOption = BitmapCacheOption . OnLoad ;
282
- image . UriSource = new Uri ( path ) ;
292
+ image . UriSource = new Uri ( path ) ;
283
293
image . CreateOptions = BitmapCreateOptions . IgnoreColorProfile ;
284
294
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
+ }
285
319
return image ;
286
320
}
287
321
}
0 commit comments