/ / BitmapImage dallo Stream restituisce 1x1px anziché l'intera immagine - c #, wpf, bitmapsource

BitmapImage from Stream restituisce 1x1px anziché l'intera immagine - c #, wpf, bitmapsource

Ho un metodo che apre a FileStream e crea a BitmapImage, usando il StreamSource proprietà.

In qualche modo, in una singola macchina, il tentativo di aprire un'immagine grande (6000x4000px) porta invece al metodo a restituire un'immagine 1x1px.

Innanzitutto ho pensato che l'immagine fosse caricata da una cartella condivisa sulla rete locale, ma era memorizzata nella cartella dei download dello stesso computer.

Ho visto che l'immagine era "bloccata / bloccata" daWindows perché è stato scaricato da una fonte non verificata, quindi ho aperto Proprietà e l'ho sbloccato. Il tentativo di caricare nuovamente l'immagine ha comportato lo stesso problema.

L'immagine è stata completamente scaricata.

Informazioni di base:

La macchina con il problema:

  • Windows 7 SP1.
  • 32 bit.
  • Net Framework 4.6.2.
  • 4 GB di memoria, con 2,5 GB in uso.

La mia macchina (funziona come previsto):

  • Windows 10, build 15063.413.
  • 64 bit.
  • Net Framework 4.7.
  • 8 GB di memoria, con 6 GB in uso.

Codice:

public static BitmapSource SourceFrom(this string fileSource, int? size = null)
{
using (var stream = new FileStream(fileSource, FileMode.Open, FileAccess.Read))
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;

if (size.HasValue)
{
//It"s not possible to get the size of image while opening, so get from another place.
var refSize = fileSource.ScaledSize(); //Gets the size of the image.

if (refSize.Height > refSize.Width)
bitmapImage.DecodePixelHeight = size.Value;
else
bitmapImage.DecodePixelWidth = size.Value;
}

bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
bitmapImage.Freeze(); //Just in case you want to load the image in another thread.
return bitmapImage;
}
}

Uso:

var image = "C:Image.jpg".SourceFrom(); //Without any other parameter.

Domanda:

  • C'è un caso in cui il mio codice non sta trattando correttamente, almeno questo spiega perché sto ottenendo un'immagine 1x1px invece di quella a dimensione intera?
  • Inoltre, perché non getta un Exception se non riesce a caricare l'immagine?

Probabile risposta:

using (var stream = new FileStream(fileSource, FileMode.Open, FileAccess.Read))
{
using (var memory = new MemoryStream())
{
stream.CopyTo(memory);
memory.Position = 0;
//...

Basta sostituire questa parte del codice e utilizzare il memory variabile al posto di stream durante l'impostazione di StreamSource oggetto.

risposte:

1 per risposta № 1

Sembra che quando il file di immagine è molto grande,o per qualche altro motivo non è possibile leggere immediatamente il flusso di origine, è necessario copiare il flusso di origine su un MemoryStream intermedio e assegnarlo al StreamSource proprietà di BitmapImage:

using (var fileStream = new FileStream(fileSource, FileMode.Open, FileAccess.Read))
using (var memoryStream = new MemoryStream())
{
fileStream.CopyTo(memoryStream);
memoryStream.Position = 0;

var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad
bitmapImage.StreamSource = memoryStream;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}