我有一个ImageSource的ImageSource的,并且要转换为字节数组或流,我怎么了?试过如何将imagesource转换为字节[]或流在uwp?
WriteableBitmap bitmap = imageSource as WriteableBitmap;
var stream = bitmap.PixelBuffer.AsStream();
但位是空...
我有一个ImageSource的ImageSource的,并且要转换为字节数组或流,我怎么了?试过如何将imagesource转换为字节[]或流在uwp?
WriteableBitmap bitmap = imageSource as WriteableBitmap;
var stream = bitmap.PixelBuffer.AsStream();
但位是空...
你应该确保ImageSource的WriteableBitmap的是它可以是BitmapImage或WriteableBitmap ......
如果imageSource是WriteableBitmap,那么您可以编写代码。
但如果ImageSource的是,你应该使用WriteableBitmapEx
第一BitmapImage的是使用的NuGet下载WriteableBitmapEx。
然后您可以将其更改为WriteableBitmap。
WriteableBitmap image = await BitmapFactory.New(1, 1).FromContent((BitmapImage).UriSource);
然后您可以将WriteableBitmap转换为流。
如果您的imageSource是RenderTargetBitmap,您可以使用此代码。
private async Task<string> ToBase64(RenderTargetBitmap bitmap)
{
var bytes = (await bitmap.GetPixelsAsync()).ToArray();
return await ToBase64(bytes, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight);
}
,如果我没有误解你的问题,你必须从你的ImageSource的存储文件,以获得一个字节数组作为结果,重新打开它的流。类似的东西在这里得到解答https://social.msdn.microsoft.com/Forums/en-US/f13a50f4-adf9-4c61-86b7-8ea02f108eac/uwpc-bitmapimage-to-byte-array?forum=wpdevelop
当然,你想获得原始像素缓冲区,或者可能得到一个编码帧,例如一个PNG? – Clemens
https://stackoverflow.com/questions/26814426/how-to-convert-imagesource-to-byte-array/47192922#47192922 –
恐怕这不适用于uwp。 – Vincent