1010
1111namespace CommonControls
1212{
13- public partial class Thumbnails : UserControl
13+ public partial class ThumbnailsNew : UserControl
1414 {
1515 // ObservableCollection automatically notifies the UI when items are added/removed
1616 public ObservableCollection < ThumbnailItem > ThumbCollection { get ; set ; } = new ( ) ;
1717
1818 public static readonly DependencyProperty ThumbCellSizeProperty =
19- DependencyProperty . Register ( nameof ( ThumbCellSize ) , typeof ( double ) , typeof ( Thumbnails ) , new PropertyMetadata ( 100.0 ) ) ;
19+ DependencyProperty . Register ( nameof ( ThumbCellSize ) , typeof ( double ) , typeof ( ThumbnailsNew ) , new PropertyMetadata ( 100.0 ) ) ;
2020
2121 public double ThumbCellSize
2222 {
2323 get => ( double ) GetValue ( ThumbCellSizeProperty ) ;
2424 set => SetValue ( ThumbCellSizeProperty , value ) ;
2525 }
2626
27- public Thumbnails ( )
27+ public ThumbnailsNew ( )
2828 {
2929 InitializeComponent ( ) ;
3030 // Set the DataContext to this control so XAML can bind to ThumbCollection
@@ -36,36 +36,34 @@ public async Task LoadImages(Dictionary<int, string> sourceList)
3636 ThumbCollection . Clear ( ) ;
3737
3838 // Using your optimized loading logic, but slightly cleaner
39- using ( var semaphore = new SemaphoreSlim ( 4 ) )
39+ using var semaphore = new SemaphoreSlim ( 4 ) ;
40+ var tasks = sourceList . Select ( async kvp =>
4041 {
41- var tasks = sourceList . Select ( async kvp =>
42+ await semaphore . WaitAsync ( ) ;
43+ try
4244 {
43- await semaphore . WaitAsync ( ) ;
44- try
45+ var img = await LoadImageAsync ( kvp . Value ) ;
46+ if ( img != null )
4547 {
46- var img = await LoadImageAsync ( kvp . Value ) ;
47- if ( img != null )
48+ // Create the Data Item
49+ var item = new ThumbnailItem
4850 {
49- // Create the Data Item
50- var item = new ThumbnailItem
51- {
52- Id = kvp . Key ,
53- FilePath = kvp . Value ,
54- ImageSource = img
55- } ;
51+ Id = kvp . Key ,
52+ FilePath = kvp . Value ,
53+ ImageSource = img
54+ } ;
5655
57- // Thread-safe add to collection
58- Application . Current . Dispatcher . Invoke ( ( ) => ThumbCollection . Add ( item ) ) ;
59- }
60- }
61- finally
62- {
63- semaphore . Release ( ) ;
56+ // Thread-safe add to collection
57+ Application . Current . Dispatcher . Invoke ( ( ) => ThumbCollection . Add ( item ) ) ;
6458 }
65- } ) ;
59+ }
60+ finally
61+ {
62+ semaphore . Release ( ) ;
63+ }
64+ } ) ;
6665
67- await Task . WhenAll ( tasks ) ;
68- }
66+ await Task . WhenAll ( tasks ) ;
6967 }
7068
7169 private async Task < BitmapSource > LoadImageAsync ( string path )
@@ -76,15 +74,13 @@ private async Task<BitmapSource> LoadImageAsync(string path)
7674 {
7775 if ( ! File . Exists ( path ) ) return null ;
7876 var bi = new BitmapImage ( ) ;
79- using ( var fs = new FileStream ( path , FileMode . Open , FileAccess . Read ) )
80- {
81- bi . BeginInit ( ) ;
82- bi . StreamSource = fs ;
83- bi . CacheOption = BitmapCacheOption . OnLoad ;
84- bi . DecodePixelWidth = ( int ) ThumbCellSize ; // Decode small to save RAM
85- bi . EndInit ( ) ;
86- bi . Freeze ( ) ; // Vital for cross-thread
87- }
77+ using var fs = new FileStream ( path , FileMode . Open , FileAccess . Read ) ;
78+ bi . BeginInit ( ) ;
79+ bi . StreamSource = fs ;
80+ bi . CacheOption = BitmapCacheOption . OnLoad ;
81+ bi . DecodePixelWidth = ( int ) ThumbCellSize ; // Decode small to save RAM
82+ bi . EndInit ( ) ;
83+ bi . Freeze ( ) ; // Vital for cross-thread
8884 return bi ;
8985 }
9086 catch { return null ; }
0 commit comments