System.IO.IOException was unhandled Message: An unhandled exception of type 'System.IO.IOException' occurred in PresentationCore.dll Additional information: Unable to read data from stream.

The error appears irregularly, sporadically. The algorithm is as follows:

  1. I collect links to pictures on the Internet, with this everything is OK
  2. for display I use ListBox which is connected with the ObservableCollection<BitmapImage> collection ObservableCollection<BitmapImage>

Here is the method that fills the collection.

 /// <summary> /// Π—Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ списка ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΎΠΊ /// </summary> /// <param name="addresses"></param> private void LoadListImages(List<string> addresses) { //будущая коллСкция ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΎΠΊ ListImages = new ObservableCollection<BitmapImage>(); //to UI MessageUser.MessageJob.Report("Π—Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ..."); try { int i = 1; BitmapImage bImage = null; foreach (var address in addresses) { //to UI MessageUser.MessageJob.Report($"Π—Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ {i}..."); //новая ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° bImage = new BitmapImage(new Uri(address, UriKind.RelativeOrAbsolute)); //Ρ€Π°Π·ΠΌΠ΅Ρ€Ρ‹ bImage.DecodePixelHeight = 100; bImage.DecodePixelWidth = 100; //ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ Π² ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡŽ ListImages.Add(bImage); //счСтчик i++; } } catch (Exception ex) { Debug.WriteLine($"{nameof(LoadListImages)} - {ex.Message}"); throw; } } 

The method works out with a bang. I even walked through the steps, and even in situations where this error occurs, but the error occurs immediately after exiting the method, that is, as I understand it, at the moment when the UI should start displaying pictures and BitmapImage starts to load on the desired link. How can this be overcome?

  • Do you have enough memory? How is ListImages tied to a ListBox, there is nothing to clean? - Alexander Muksimov
  • @Alexander Muksimov at the start of the program takes approx. 100MB, after collecting links approx. 135MB, if this error does not occur, and the pictures are loaded, then about 350MB. It is not necessary to clean anything, because ListImages = new ObservableCollection<BitmapImage>(); - Bulson
  • And yet, before loading (since the defect is floating), I would do ListImages.Cleare () and GC.Collect (). According to the code you gave, I do not see anything else criminal - Alexander Muksimov

1 answer 1

The problem was solved by some rewriting in the asynchronous style, plus the refusal to directly download the image to the address using the BitmapImage itself, and the use of HttpClient for this. Well, in general, the pictures began to appear (load) gradually, one by one, and not in a crowd as soon as it was before.

  /// <summary> /// Π—Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ списка ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΎΠΊ /// </summary> /// <param name="addresses"></param> private async Task LoadListImagesAsync(List<string> addresses) { //будущая коллСкция ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΎΠΊ ListImages = new ObservableCollection<BitmapImage>(); //to UI MessageUser.MessageJob.Report("Π—Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ..."); //ΠΎΠ±Ρ‰Π΅Π΅ количСство Π·Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌΡ‹Ρ… ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΎΠΊ string count = addresses.Count.ToString(); try { int i = 1; BitmapImage bImage = null; foreach (var address in addresses) { //to UI MessageUser.MessageJob.Report($"Π—Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ: {i} ΠΈΠ· {count}..."); //новая ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° Uri pathPicture = new Uri(address, UriKind.RelativeOrAbsolute); bImage = await LoadBitmapImageAsync(pathPicture); //ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ Π² ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡŽ ListImages.Add(bImage); //счСтчик i++; } } catch (Exception ex) { Debug.WriteLine($"{nameof(LoadListImagesAsync)} - {ex.Message}"); throw; } } /// <summary> /// Π—Π°Π³Ρ€ΡƒΠ·ΠΊΠ° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ ΠΈΠ· адрСса /// </summary> /// <param name="pathPicture">адрСс ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ</param> /// <returns></returns> private async Task<BitmapImage> LoadBitmapImageAsync(Uri pathPicture) { BitmapImage result = new BitmapImage(); //Ρ€Π°Π·ΠΌΠ΅Ρ€Ρ‹ result.DecodePixelHeight = 100; result.DecodePixelWidth = 100; try { using (HttpClient client = new HttpClient()) using (var response = await client.GetAsync(pathPicture)) { response.EnsureSuccessStatusCode(); result.BeginInit(); result.CacheOption = BitmapCacheOption.OnLoad; result.StreamSource = await response.Content.ReadAsStreamAsync(); result.EndInit(); } } catch (Exception ex) { Debug.WriteLine($"{nameof(LoadBitmapImageAsync)} - {ex.Message}"); return new BitmapImage(); } return result; } 
  • Well, asynchronous simultaneous loading can be done with async / await - VladD
  • @VladD, apparently I was somewhat hasty with conclusions ... Now I’ve tested it and in the Diagnostic Tools in Events I discovered the periodic appearance of various errors related to downloading from the Internet, apparently, some websites do not load well or do not give pictures at all. But at least the application does not fall, but instead of the erroneous picture, the white field remains. It suits me for now. - Bulson
  • As for an asynchronous download, something like this: pastebin.com/VM34nE1f - VladD
  • @VladD yeah, thanks - Bulson