我有一個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