我有个问题。我想转换BitmapImage
成byte[]
数组并返回。
我写了这些方法:
public static byte[] ToByteArray(this BitmapImage bitmapImage)
{
byte[] bytes;
using (MemoryStream ms = new MemoryStream())
{
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource.CopyTo(ms);
bitmapImage.EndInit();
bytes = ms.ToArray();
}
return bytes;
}
public static BitmapImage ToBitmapImage(this byte[] bytes, int width, int height)
{
BitmapImage bitmapImage = new BitmapImage();
using (MemoryStream ms = new MemoryStream(bytes))
{
ms.Position = 0;
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = ms;
bitmapImage.EndInit(); // HERE'S AN EXCEPTION!!!
}
return bitmapImage;
}
第一个工作正常,但是当我尝试从转换为时,byte[]
我BitmapImage
得到了NotSupportedException
……为什么?如何更正第二种方法的代码?