99// ReSharper disable MemberCanBeInternal
1010// ReSharper disable MemberCanBePrivate.Global
1111// ReSharper disable EventNeverSubscribedTo.Global
12- // ReSharper disable EventNeverSubscribedTo.Global
1312// ReSharper disable UnusedAutoPropertyAccessor.Global
1413// ReSharper disable UnusedType.Global
1514// ReSharper disable UnusedMember.Global
@@ -656,41 +655,33 @@ await Application.Current.Dispatcher.InvokeAsync(() =>
656655 /// <param name="width">The width.</param>
657656 /// <param name="height">The height.</param>
658657 /// <returns>The loaded and resized Image</returns>
659- private static async Task < BitmapImage > GetBitmapImageFileStreamAsync ( string filePath , int width , int height )
658+ private static async Task < BitmapImage ? > GetBitmapImageFileStreamAsync ( string filePath , int width , int height )
660659 {
661- return string . IsNullOrWhiteSpace ( filePath ) || ! File . Exists ( filePath )
662- ? null
663- : await Task . Run ( ( ) =>
664- {
665- BitmapImage bitmapImage = null ;
660+ if ( string . IsNullOrWhiteSpace ( filePath ) || ! File . Exists ( filePath ) ) return null ;
666661
667- Application . Current . Dispatcher . Invoke ( ( ) =>
662+ return await Task . Run ( ( ) =>
663+ {
664+ try
665+ {
666+ var bitmapImage = new BitmapImage ( ) ;
667+ using ( var stream = new FileStream ( filePath , FileMode . Open , FileAccess . Read , FileShare . Read ) )
668668 {
669- FileStream stream = null ;
670- try
671- {
672- bitmapImage = new BitmapImage ( ) ;
673- stream = new FileStream ( filePath , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
674- bitmapImage . BeginInit ( ) ;
675- bitmapImage . CacheOption = BitmapCacheOption . OnLoad ;
676- bitmapImage . DecodePixelWidth = width ;
677- bitmapImage . DecodePixelHeight = height ;
678- bitmapImage . StreamSource = stream ;
679- bitmapImage . EndInit ( ) ;
680- bitmapImage . Freeze ( ) ;
681- }
682- catch ( Exception ex )
683- {
684- Trace . WriteLine ( $ "{ ComCtlResources . ErrorCouldNotLoadImage } { ex . Message } ") ;
685- }
686- finally
687- {
688- stream ? . Dispose ( ) ;
689- }
690- } ) ;
691-
669+ bitmapImage . BeginInit ( ) ;
670+ bitmapImage . CacheOption = BitmapCacheOption . OnLoad ;
671+ bitmapImage . DecodePixelWidth = width ;
672+ // bitmapImage.DecodePixelHeight = height; // Preserving aspect ratio usually requires setting only one dimension
673+ bitmapImage . StreamSource = stream ;
674+ bitmapImage . EndInit ( ) ;
675+ bitmapImage . Freeze ( ) ; // Crucial: Makes it accessible to the UI thread
676+ }
692677 return bitmapImage ;
693- } ) ;
678+ }
679+ catch ( Exception ex )
680+ {
681+ Trace . WriteLine ( $ "{ ComCtlResources . ErrorCouldNotLoadImage } { ex . Message } ") ;
682+ return null ;
683+ }
684+ } ) ;
694685 }
695686
696687 /// <summary>
@@ -990,7 +981,6 @@ private void Dispose(bool disposing)
990981 _disposed = true ;
991982 }
992983
993-
994984 /// <summary>
995985 /// Finalizes this instance.
996986 /// </summary>
@@ -999,4 +989,4 @@ private void Dispose(bool disposing)
999989 Dispose ( false ) ;
1000990 }
1001991 }
1002- }
992+ }
0 commit comments