@@ -312,6 +312,7 @@ internal static void UpdateSourcePaths(ObjectInfo objInfo) {
312312 case FileFlags . Archive :
313313 LoadFile ( new LoadOptions ( objInfo . FileSystemPath ) {
314314 Flags = FileFlags . Archive ,
315+ LoadImage = false ,
315316 ObjInfoCallback = oi => objInfo . SourcePaths = oi . SourcePaths ,
316317 } ) ;
317318 break ;
@@ -377,25 +378,20 @@ public static ImageSource GetImageSource(ObjectInfo objInfo, int sourcePathIdx,
377378 }
378379 break ;
379380 case FileFlags . Archive :
381+ case FileFlags . Archive | FileFlags . Image :
380382 if ( objInfo . SourcePaths ? . Length > 0 ) {
381383 LoadFile ( new LoadOptions ( objInfo . FileSystemPath ) {
382384 DecodeSize = decodeSize ,
383385 LoadImage = true ,
384386 TryCache = tryCache ,
385387 FileNames = new [ ] { objInfo . SourcePaths [ sourcePathIdx ] } ,
386- Flags = FileFlags . Archive ,
388+ Flags = objInfo . Flags ,
387389 CldInfoCallback = oi => source = oi . ImageSource ,
388390 ObjInfoCallback = oi => objInfo . Flags = oi . Flags
389391 } ) ;
390392 }
391393 if ( source == null ) {
392- source = App . fa_archive ;
393- }
394- break ;
395- case FileFlags . Archive | FileFlags . Image :
396- source = objInfo . ImageSource ;
397- if ( source == null ) {
398- source = App . fa_image ;
394+ source = objInfo . Flags . HasFlag ( FileFlags . Image ) ? App . fa_image : App . fa_archive ;
399395 }
400396 break ;
401397 }
@@ -455,89 +451,95 @@ public static void UpdateImageInfo(Stream stream, ImageInfo imgInfo) {
455451 /// <para>A <paramref name="decodeSize"/> higher than the actual resolution will be ignored.
456452 /// Note that this is the size in pixel instead of the device-independent size used in WPF.</para>
457453 /// </summary>
454+ /// <returns>Returns null if error occured.</returns>
458455 public static BitmapSource GetImageSource ( Stream stream , SizeInt decodeSize = default ) {
459- stream . Position = 0 ;
460- var frame = BitmapFrame . Create ( stream , BitmapCreateOptions . DelayCreation , BitmapCacheOption . None ) ;
461- var pixelSize = new SizeInt ( frame . PixelWidth , frame . PixelHeight ) ;
462- ushort orien = 0 ;
463- if ( ( frame . Metadata as BitmapMetadata ) ? . GetQuery ( "/app1/ifd/{ushort=274}" ) is ushort u )
464- orien = u ;
465- frame = null ;
466-
467- //calculate decode size
468- if ( decodeSize . Width + decodeSize . Height > 0 ) {
469- //use pixelSize if decodeSize is too big
470- //DecodePixelWidth / Height is set to PixelWidth / Height anyway in reference source
471- if ( decodeSize . Width > pixelSize . Width ) decodeSize . Width = pixelSize . Width ;
472- if ( decodeSize . Height > pixelSize . Height ) decodeSize . Height = pixelSize . Height ;
473-
474- //flip decodeSize according to orientation
475- if ( orien > 4 && orien < 9 )
476- decodeSize = new SizeInt ( decodeSize . Height , decodeSize . Width ) ;
477- }
456+ try {
457+ stream . Position = 0 ;
458+ var frame = BitmapFrame . Create ( stream , BitmapCreateOptions . DelayCreation , BitmapCacheOption . None ) ;
459+ var pixelSize = new SizeInt ( frame . PixelWidth , frame . PixelHeight ) ;
460+ ushort orien = 0 ;
461+ if ( ( frame . Metadata as BitmapMetadata ) ? . GetQuery ( "/app1/ifd/{ushort=274}" ) is ushort u )
462+ orien = u ;
463+ frame = null ;
464+
465+ //calculate decode size
466+ if ( decodeSize . Width + decodeSize . Height > 0 ) {
467+ //use pixelSize if decodeSize is too big
468+ //DecodePixelWidth / Height is set to PixelWidth / Height anyway in reference source
469+ if ( decodeSize . Width > pixelSize . Width ) decodeSize . Width = pixelSize . Width ;
470+ if ( decodeSize . Height > pixelSize . Height ) decodeSize . Height = pixelSize . Height ;
471+
472+ //flip decodeSize according to orientation
473+ if ( orien > 4 && orien < 9 )
474+ decodeSize = new SizeInt ( decodeSize . Height , decodeSize . Width ) ;
475+ }
478476
479- //init bitmapimage
480- stream . Position = 0 ;
481- var bi = new BitmapImage ( ) ;
482- bi . BeginInit ( ) ;
483- bi . CacheOption = BitmapCacheOption . OnLoad ;
484- if ( pixelSize . Width > 0 && pixelSize . Height > 0 ) {
485- //setting both DecodePixelWidth and Height will break the aspect ratio
486- var imgRatio = ( double ) pixelSize . Width / pixelSize . Height ;
487- if ( decodeSize . Width > 0 && decodeSize . Height > 0 ) {
488- if ( imgRatio > ( double ) decodeSize . Width / decodeSize . Height )
477+ //init bitmapimage
478+ stream . Position = 0 ;
479+ var bi = new BitmapImage ( ) ;
480+ bi . BeginInit ( ) ;
481+ bi . CacheOption = BitmapCacheOption . OnLoad ;
482+ if ( pixelSize . Width > 0 && pixelSize . Height > 0 ) {
483+ //setting both DecodePixelWidth and Height will break the aspect ratio
484+ var imgRatio = ( double ) pixelSize . Width / pixelSize . Height ;
485+ if ( decodeSize . Width > 0 && decodeSize . Height > 0 ) {
486+ if ( imgRatio > ( double ) decodeSize . Width / decodeSize . Height )
487+ bi . DecodePixelHeight = decodeSize . Height ;
488+ else
489+ bi . DecodePixelWidth = decodeSize . Width ;
490+ }
491+ else if ( decodeSize . Width == 0 && decodeSize . Height > 0 )
489492 bi . DecodePixelHeight = decodeSize . Height ;
490- else
493+ else if ( decodeSize . Height == 0 && decodeSize . Width > 0 )
491494 bi . DecodePixelWidth = decodeSize . Width ;
492495 }
493- else if ( decodeSize . Width == 0 && decodeSize . Height > 0 )
494- bi . DecodePixelHeight = decodeSize . Height ;
495- else if ( decodeSize . Height == 0 && decodeSize . Width > 0 )
496- bi . DecodePixelWidth = decodeSize . Width ;
497- }
498- bi . StreamSource = stream ;
499- bi . EndInit ( ) ;
500- bi . Freeze ( ) ;
501-
502- if ( orien < 2 ) return bi ;
503- //apply orientation based on metadata
504- var tb = new TransformedBitmap ( ) ;
505- tb . BeginInit ( ) ;
506- tb . Source = bi ;
507- switch ( orien ) {
508- case 2 :
509- tb . Transform = new ScaleTransform ( - 1d , 1d ) ;
510- break ;
511- case 3 :
512- tb . Transform = new RotateTransform ( 180d ) ;
513- break ;
514- case 4 :
515- tb . Transform = new ScaleTransform ( 1d , - 1d ) ;
516- break ;
517- case 5 : {
518- var tg = new TransformGroup ( ) ;
519- tg . Children . Add ( new RotateTransform ( 90d ) ) ;
520- tg . Children . Add ( new ScaleTransform ( - 1d , 1d ) ) ;
521- tb . Transform = tg ;
496+ bi . StreamSource = stream ;
497+ bi . EndInit ( ) ;
498+ bi . Freeze ( ) ;
499+
500+ if ( orien < 2 ) return bi ;
501+ //apply orientation based on metadata
502+ var tb = new TransformedBitmap ( ) ;
503+ tb . BeginInit ( ) ;
504+ tb . Source = bi ;
505+ switch ( orien ) {
506+ case 2 :
507+ tb . Transform = new ScaleTransform ( - 1d , 1d ) ;
522508 break ;
523- }
524- case 6 :
525- tb . Transform = new RotateTransform ( 90d ) ;
526- break ;
527- case 7 : {
528- var tg = new TransformGroup ( ) ;
529- tg . Children . Add ( new RotateTransform ( 90d ) ) ;
530- tg . Children . Add ( new ScaleTransform ( 1d , - 1d ) ) ;
531- tb . Transform = tg ;
509+ case 3 :
510+ tb . Transform = new RotateTransform ( 180d ) ;
532511 break ;
533- }
534- case 8 :
535- tb . Transform = new RotateTransform ( 270d ) ;
536- break ;
512+ case 4 :
513+ tb . Transform = new ScaleTransform ( 1d , - 1d ) ;
514+ break ;
515+ case 5 : {
516+ var tg = new TransformGroup ( ) ;
517+ tg . Children . Add ( new RotateTransform ( 90d ) ) ;
518+ tg . Children . Add ( new ScaleTransform ( - 1d , 1d ) ) ;
519+ tb . Transform = tg ;
520+ break ;
521+ }
522+ case 6 :
523+ tb . Transform = new RotateTransform ( 90d ) ;
524+ break ;
525+ case 7 : {
526+ var tg = new TransformGroup ( ) ;
527+ tg . Children . Add ( new RotateTransform ( 90d ) ) ;
528+ tg . Children . Add ( new ScaleTransform ( 1d , - 1d ) ) ;
529+ tb . Transform = tg ;
530+ break ;
531+ }
532+ case 8 :
533+ tb . Transform = new RotateTransform ( 270d ) ;
534+ break ;
535+ }
536+ tb . EndInit ( ) ;
537+ tb . Freeze ( ) ;
538+ return tb ;
539+ }
540+ catch {
541+ return null ;
537542 }
538- tb . EndInit ( ) ;
539- tb . Freeze ( ) ;
540- return tb ;
541543 }
542544
543545 public static void CacheFolder ( string folderPath , ref CancellationTokenSource tknSrc , object tknLock , Action < string , int , int > callback ) {
@@ -587,29 +589,6 @@ public static void CacheFolder(string folderPath, ref CancellationTokenSource tk
587589 tknSrc = null ;
588590 Monitor . Exit ( tknLock ) ;
589591 }
590-
591- // while (cacheThreadIdx < ObjectList.Count) {
592- // while (tknSrc_LoadThumb != null || LoadThrottle.CurrentCount <= 1) {
593- // if (cacheThreadExit) break;
594- // Thread.Sleep(2000);
595- // }
596-
597- // var objInfo = ObjectList[cacheThreadIdx];
598- // var decodeSize = (SizeInt)Setting.ThumbnailSize;
599- // if (objInfo.SourcePaths == null) UpdateSourcePaths(objInfo);
600- // var path = objInfo.Flags.HasFlag(FileFlags.Archive) ?
601- // Path.Combine(objInfo.FileSystemPath, objInfo.SourcePaths[0]) :
602- // objInfo.SourcePaths[0];
603- // if (!SQLiteHelper.ThumbExistInDB(path, decodeSize)) {
604- //#if DEBUG
605- // Console.WriteLine($"Caching to DB: {path}");
606- //#endif
607- // GetImageSource(objInfo, 0, decodeSize, false);
608- // }
609-
610- // if (cacheThreadExit) break;
611- // cacheThreadIdx += 1;
612- // }
613592 }
614593 }
615594}
0 commit comments