在Windows Phone 8.1 Runtime中将BitmapImage转换为byte []数组

Bay*_*ern 2 c# bytearray bitmapimage windows-runtime windows-phone-8.1

有一些示例可以执行此操作,但它们适用于Windows Phone 8.0或8.1 Silverlight.

但是如何为Windows Phone 8.1 Runtime执行此操作?

Rob*_*SFT 7

您无法从Windows.UI.Xaml.Media.Imaging中提取像素.BitmapImage.

最通用的解决方案是使用WriteableBitmap而不是BitmapImage.这些类都是BitmapSources,几乎可以互换使用.的WriteableBitmap的提供通过其它的像素数据访问PixelBuffer属性:

byte[] pixelArray = myWriteableBitmap.PixelBuffer.ToArray(); // convert to Array
Stream pixelStream = wb.PixelBuffer.AsStream();  // convert to stream
Run Code Online (Sandbox Code Playgroud)

否则,您需要从BitmapImage从中获取像素.根据BitmapImage的初始化方式,您可以从其UriSource属性中找到它的来源.所述的WinRT的Xaml工具包具有扩展方法FromBitmapImage从根据它的一个UriSource的BitmapImage创建WriteableBitmap的.

一个丑陋的选择是将BitmapImage渲染为Image,基于Image创建RenderTargetBitmap,然后使用RenderTargetBitmap获取其Pixels.CopyPixelsAsync()