c# - 在 Windows Phone 8.1 运行时将 BitmapImage 转换为 byte[] 数组

标签 c# windows-runtime windows-phone-8.1 arrays bitmapimage

有几个示例可以执行此操作,但它们适用于 Windows Phone 8.0 或 8.1 Silverlight。

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

最佳答案

您无法从 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

否则,您将需要从 BitmapImage 从哪里获取像素。根据 BitmapImage 的初始化方式,您可以从其 UriSource 中找到它的来源。属性(property)。 WinRT Xaml Toolkit有一个 extension method FromBitmapImage根据其 UriSource 从 BitmapImage 创建 WriteableBitmap。

一个丑陋的选择是将 BitmapImage 渲染成图像,创建一个 RenderTargetBitmap基于图像,然后使用 RenderTargetBitmap 获取其像素。 CopyPixelsAsync ()

关于c# - 在 Windows Phone 8.1 运行时将 BitmapImage 转换为 byte[] 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34184855/

相关文章:

c# - 如何在 WP 8.1 中加载时禁用页面动画?

c# - 如何将文本 block 可见性绑定(bind)到另一个文本 block 文本属性

c# - 用于绑定(bind)的 xaml 术语的差异

c# - App挂起后恢复相机资源

c# - 使用单个表达式在数据表中创建列

xaml - Windows 8 Metro : Implementing Validation

windows-runtime - Win 8.1 SearchBox - 绑定(bind)建议

c# - 如何检测滚动查看器是否在 winrt 中到达底部

c# - Visual Studio 2010 调试器(x86 模式)未显示未处理的错误

C# 无法访问外部类型的非静态成员