Domanda

Sto cercando di creare un oggetto di immagine con un array di byte come fonte. Che cosa sto facendo di sbagliato?

Un'eccezione viene generata quando si tenta di inizializzare l'oggetto immagine con un array di byte come dati di origine. L'eccezione è mostrato nel mio codice, qui di seguito.

public class MyClass
{
    publuc System.Windows.Media.Imaging.BitmapImage InstanceImage { get; set; }

    public void GetImage()
    {
        // Retrieves a list of custom "Item" objects that contain byte arrays.
        // ScvClnt is our service client. The PollQueue method is designed to return information to us.
        lstQueue = SvcClnt.PollQueue(1);

        // This condition always evaluates as True, since we requested exactly 1 "Item" from the service client.
        if (lstQueue.Count == 1)
        {
            // lstQueue[0].InstanceImage is a byte array containing the data from an image file.
            // I have confirmed that it is a valid TIFF image file, by writing it to disk and opening it in MSPaint.
            if (lstQueue[0].InstanceImage != null)
            {
                // This condition is also True, since the image is just under 3KB.
                if (lstQueue[0].InstanceImage.Length > 0)
                {
                    this.InstanceImage = new System.Windows.Media.Imaging.BitmapImage();
                    this.InstanceImage.BeginInit();
                    this.InstanceImage.StreamSource = new System.IO.MemoryStream(lstQueue[0].InstanceImage);
                    InstanceImage.EndInit();
                    // The call to EndInit throws a NullReferenceException.
                    // {"Object reference not set to an instance of an object."}
                    // I have confirmed that this.InstanceImage and this.InstanceImage.StreamSource are not null at this point.
                    // They are successfully assigned in the lines of code above.
                } else InstanceImage = null;
            } else InstanceImage = null;
        } else InstanceImage = null;
    }
}

Non ho idea di quello che sulla Terra potrebbe essere andato storto.
Qualche consiglio sarebbe molto apprezzato.

È stato utile?

Soluzione 2

Questo forum MSDN alberino utilizza questo esempio come una soluzione.

using (MemoryStream stream = new MemoryStream(abyteArray0))
{
    image.Source = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}

//The field image should be of type System.Windows.Controls.Image.

Ho usato il metodo BitmapFrame.Create che richiede solo il flusso come parametro, e ha funzionato come un fascino.

Altri suggerimenti

Non sono sicuro che sto seguendo quello che stai cercando di fare con la classe a prima vista, ma per affrontare la domanda iniziale, dare a questo un colpo:

    public static Image ConvertByteArrayToImage(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);
        return returnImage;
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top