c# - 在 UWP 中将 BitmapImage 转换为字节数组

标签 c# winrt-xaml win-universal-app bitmapimage

如何在 UWP 中将 BitmapImage 对象转换为字节数组?在 .Net 中很容易实现,即使在以前的 WinRT 版本中,在整个互联网上查找但没有成功,其中一种解决方案是使用 WriteableBitmap,如本 answer 中所述,但在当前版本的 UWP 中,不可能从 BitmapImage 构建 WriteableBitmap,是否有解决方法?

最佳答案

由于您是从图片网址开始的,所以我唯一能想到的方法就是获取图片流。为此,RandomAccessStreamReference.CreateFromUri() 方法非常有用。

Windows.Storage.Streams.IRandomAccessStream random = await Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(new Uri("http://...")).OpenReadAsync();

然后我们必须解码流以便能够读取所有像素供以后使用。

Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(random);
Windows.Graphics.Imaging.PixelDataProvider pixelData = await decoder.GetPixelDataAsync();

终于可以通过这种方式访问​​像素缓冲区了。

byte[] bytes = pixelData.DetachPixelData();

关于c# - 在 UWP 中将 BitmapImage 转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36108056/

相关文章:

c# - 获取 Windows.Ui.Xaml.DependencyProperty 的名称

c# - 在 C++/CLI 中创建托管类和命名空间时出现问题

c# - 为什么 Thread.Join() 挂起,即使线程中的方法已经返回?

c# - 什么时候自动复制记录结构?

c# - C# 事件处理程序应该是异常安全的吗?

c# - 如何处理硬件按钮。在 UWP 中按下后退?

c# - Microsoft.NET.CoreRuntime 的 SOS 调试扩展

windows-runtime - 在 Windows 应用商店应用程序上实现带下划线的超链接的最佳方法?

windows-store-apps - 错误 : "' Base' is not a valid PriceId for base price"when trying to deploy a Microsoft Store app with VSTS tasks

xaml - 如何检测到 Windows Mobile 已转换为连续模式?