/// <inheritdoc /> protected override async Task <BitmapSource> CreateAsync(string key, CancellationToken cancellationToken = new CancellationToken()) { string path = Path.Combine("TestFiles", key); if (!File.Exists(path)) { string location = Assembly.GetExecutingAssembly().Location; if (location != null) { path = Path.Combine(Path.GetDirectoryName(location), "TestFiles", key); } } if (!File.Exists(path)) { Log.Error().WriteLine("Couldn't find location {0}", path); // What is the default here? return(null); } using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { // ReSharper disable once AccessToDisposedClosure return(await Task.Run(() => stream.BitmapImageFromStream(), cancellationToken).ConfigureAwait(false)); } }
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) { foreach (var fileName in Directory.GetFiles(ImagesFolder)) { using (var stream = new FileStream(fileName, FileMode.Open)) { Images.Add(stream.BitmapImageFromStream()); } } }