2017-10-11 40 views
-1

我有一个ImageSource的ImageSource的,并且要转换为字节数组或流,我怎么了?试过如何将imagesource转换为字节[]或流在uwp?

WriteableBitmap bitmap = imageSource as WriteableBitmap; 
var stream = bitmap.PixelBuffer.AsStream(); 

但位是空...

+0

当然,你想获得原始像素缓冲区,或者可能得到一个编码帧,例如一个PNG? – Clemens

+0

https://stackoverflow.com/questions/26814426/how-to-convert-imagesource-to-byte-array/47192922#47192922 –

+0

恐怕这不适用于uwp。 – Vincent

回答

0

你应该确保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); 
} 

参见:http://lindexi.oschina.io/lindexi//post/win10-uwp-%E8%AF%BB%E5%8F%96%E4%BF%9D%E5%AD%98WriteableBitmap-BitmapImage/