Image I';我试图创建一个以字节数组为源的图像对象。我做错了什么?
我正在尝试创建一个源代码为字节数组的图像对象。我做错了什么 当我尝试使用字节数组作为源数据初始化image对象时,会引发异常。下面我的代码中显示了该异常Image I';我试图创建一个以字节数组为源的图像对象。我做错了什么?,image,.net-3.5,nullreferenceexception,memorystream,bitmapimage,Image,.net 3.5,Nullreferenceexception,Memorystream,Bitmapimage,我正在尝试创建一个源代码为字节数组的图像对象。我做错了什么 当我尝试使用字节数组作为源数据初始化image对象时,会引发异常。下面我的代码中显示了该异常 public class MyClass { publuc System.Windows.Media.Imaging.BitmapImage InstanceImage { get; set; } public void GetImage() { // Retrieves a list of custo
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;
}
我不确定我是否第一眼就明白了你想对你的班级做什么,但为了解决最初的问题,请尝试一下:
public static Image ConvertByteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
使用此示例作为解决方案
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方法,该方法只将流作为参数,它的工作方式很有魅力。使用此示例作为解决方案
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方法,该方法只将流作为一个参数,它的工作非常出色。我相信这是.NET 2.0或类似版本的解决方案。我正在使用WPF在.NET3.5中工作。因此,我不使用System.Drawing。我相信这是针对.NET 2.0或类似版本的解决方案。我正在使用WPF在.NET3.5中工作。因此,我没有使用System.Drawing