私は、そのソースとしてバイト配列をImageオブジェクトを作成しようとしています。私は間違って何をやっていますか?
-
19-09-2019 - |
質問
私は、そのソースとしてバイト配列にイメージオブジェクトを作成しようとしています。私が間違って何をやっていますか?
Iは、ソースデータとしてのバイトのアレイと画像オブジェクトを初期化しようとすると、例外がスローされます。例外は、以下の、私のコードに示されています。
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;
}
}
私は地球上で何がおそらく間違って行くことができないアイデアを持っていません。
何かアドバイスをいただければ幸いです。
解決 2
このMSDNフォーラムポストには、ソリューションとしてこの例を使用しています。
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メソッドを使用し、それが魅力のように働いています。
他のヒント
私は、私はあなたが一目見ただけで、あなたのクラスに何をしようとして、次のよわからないんだけど、元の質問に対処するために、本に打撃与える:
public static Image ConvertByteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
所属していません StackOverflow