0

바이트 배열을 소스로 사용하여 이미지 객체를 만들려고합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?바이트 배열을 소스로 사용하여 이미지 객체를 만들려고합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

원본 데이터로 바이트 배열을 사용하여 이미지 개체를 초기화하려고하면 예외가 throw됩니다. 아래 예외는 내 코드에 나와 있습니다.

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; 
    } 
} 

지구상에 어떤 것이 잘못 될지 모릅니다.
조언을 주시면 감사하겠습니다.

public static Image ConvertByteArrayToImage(byte[] byteArrayIn) 
    { 
     MemoryStream ms = new MemoryStream(byteArrayIn); 
     Image returnImage = Image.FromStream(ms); 
     return returnImage; 
    } 

답변

2

This MSDN forum post이 예제를 사용 : 당신은 첫눈에 당신의 클래스를하려고 노력하고 있지만 원래의 질문을 해결하기 위해 무엇을 다음있어 확실하지 않다

3

이에게 주사를 솔루션으로.

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. 

나는 스트림만을 매개 변수로 사용하는 BitmapFrame.Create 메서드를 사용했으며 매력처럼 작동했습니다.

+0

흥미로운 .... Lemme이 기회를 제공합니다. – Giffyguy

+0

이것이 .NET 2.0 또는 그와 비슷한 솔루션입니다. WPF를 사용하는 .NET 3.5에서 일하고 있습니다. 따라서 System.Drawing을 사용하지 않습니다. – Giffyguy

관련 문제