바이트 배열을 소스로 사용하여 이미지 객체를 만들려고합니다. 내가 도대체 뭘 잘못하고있는 겁니까?바이트 배열을 소스로 사용하여 이미지 객체를 만들려고합니다. 내가 도대체 뭘 잘못하고있는 겁니까?
원본 데이터로 바이트 배열을 사용하여 이미지 개체를 초기화하려고하면 예외가 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;
}
흥미로운 .... Lemme이 기회를 제공합니다. – Giffyguy
이것이 .NET 2.0 또는 그와 비슷한 솔루션입니다. WPF를 사용하는 .NET 3.5에서 일하고 있습니다. 따라서 System.Drawing을 사용하지 않습니다. – Giffyguy