本文介绍了在 UWP 中将 BitmapImage 转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 UWP 中将 BitmapImage 对象转换为字节数组?在 .Net 中很容易实现,即使在以前的 WinRT 版本中,在互联网上查看但没有成功,解决方案之一是使用 ,但在当前版本的 UWP 中,从 BitmapImage 构建 WriteableBitmap 是不可能的,有什么解决办法吗?

How can I convert a BitmapImage object to byte array in UWP ? in .Net it is easy to achieve, even in previous WinRT versions, looked all over the internet but without success, one of the solutions was to use a WriteableBitmap like mentioned in this answer, but in the current version of UWP, constructing a WriteableBitmap out of a BitmapImage isn't possible, any work around ?

推荐答案

既然你是从图像 url 开始的,我唯一能想到的方法就是获取图像的流.为此,RandomAccessStreamReference.CreateFromUri() 方法非常有用.

Since you start from image url, the only way I can figure out is to get the stream of the image. To do this, RandomAccessStreamReference.CreateFromUri() method is really useful.

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

然后我们必须对流进行解码,以便能够读取所有像素以备后用.

Then we have to decode the stream in order to be able to read all pixels for later usage.

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

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

Finally you can access pixel buffer in such a way.

byte[] bytes = pixelData.DetachPixelData();

这篇关于在 UWP 中将 BitmapImage 转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 00:38
查看更多