【发布时间】:2011-01-19 07:35:26
【问题描述】:
我想在 Windows Phone 7 应用程序中将 BitmapImage 转换为 ByteArray。所以我尝试了这个,但它抛出了运行时异常“无效指针异常”。谁能解释为什么我正在尝试做的事情会引发异常。您能否为此提供替代解决方案。
public static byte[] ConvertToBytes(this BitmapImage bitmapImage)
{
byte[] data;
// Get an Image Stream
using (MemoryStream ms = new MemoryStream())
{
WriteableBitmap btmMap = new WriteableBitmap(bitmapImage);
// write an image into the stream
Extensions.SaveJpeg(btmMap, ms,
bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);
// reset the stream pointer to the beginning
ms.Seek(0, 0);
//read the stream into a byte array
data = new byte[ms.Length];
ms.Read(data, 0, data.Length);
}
//data now holds the bytes of the image
return data;
}
【问题讨论】:
标签: c# windows-phone-7