|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Collections.ObjectModel; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Windows; |
| 8 | +using System.Windows.Controls; |
| 9 | +using System.Windows.Media.Imaging; |
| 10 | + |
| 11 | +namespace CommonControls |
| 12 | +{ |
| 13 | + public partial class Thumbnails : UserControl |
| 14 | + { |
| 15 | + // ObservableCollection automatically notifies the UI when items are added/removed |
| 16 | + public ObservableCollection<ThumbnailItem> ThumbCollection { get; set; } = new(); |
| 17 | + |
| 18 | + public static readonly DependencyProperty ThumbCellSizeProperty = |
| 19 | + DependencyProperty.Register(nameof(ThumbCellSize), typeof(double), typeof(Thumbnails), new PropertyMetadata(100.0)); |
| 20 | + |
| 21 | + public double ThumbCellSize |
| 22 | + { |
| 23 | + get => (double)GetValue(ThumbCellSizeProperty); |
| 24 | + set => SetValue(ThumbCellSizeProperty, value); |
| 25 | + } |
| 26 | + |
| 27 | + public Thumbnails() |
| 28 | + { |
| 29 | + InitializeComponent(); |
| 30 | + // Set the DataContext to this control so XAML can bind to ThumbCollection |
| 31 | + DataContext = this; |
| 32 | + } |
| 33 | + |
| 34 | + public async Task LoadImages(Dictionary<int, string> sourceList) |
| 35 | + { |
| 36 | + ThumbCollection.Clear(); |
| 37 | + |
| 38 | + // Using your optimized loading logic, but slightly cleaner |
| 39 | + using (var semaphore = new SemaphoreSlim(4)) |
| 40 | + { |
| 41 | + var tasks = sourceList.Select(async kvp => |
| 42 | + { |
| 43 | + await semaphore.WaitAsync(); |
| 44 | + try |
| 45 | + { |
| 46 | + var img = await LoadImageAsync(kvp.Value); |
| 47 | + if (img != null) |
| 48 | + { |
| 49 | + // Create the Data Item |
| 50 | + var item = new ThumbnailItem |
| 51 | + { |
| 52 | + Id = kvp.Key, |
| 53 | + FilePath = kvp.Value, |
| 54 | + ImageSource = img |
| 55 | + }; |
| 56 | + |
| 57 | + // Thread-safe add to collection |
| 58 | + Application.Current.Dispatcher.Invoke(() => ThumbCollection.Add(item)); |
| 59 | + } |
| 60 | + } |
| 61 | + finally |
| 62 | + { |
| 63 | + semaphore.Release(); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + await Task.WhenAll(tasks); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private async Task<BitmapSource> LoadImageAsync(string path) |
| 72 | + { |
| 73 | + return await Task.Run(() => |
| 74 | + { |
| 75 | + try |
| 76 | + { |
| 77 | + if (!File.Exists(path)) return null; |
| 78 | + 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 | + } |
| 88 | + return bi; |
| 89 | + } |
| 90 | + catch { return null; } |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments