如何从 StorageFile 获取 IRandomAccessStream [英] How to get IRandomAccessStream from StorageFile

查看:16
本文介绍了如何从 StorageFile 获取 IRandomAccessStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我声明

private MediaCapture _mediaCapture;
StorageFile capturedPhoto;
IRandomAccessStream imageStream;

第二个我正在捕获

var lowLagCapture = await _mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));

var capturedPhoto = await lowLagCapture.CaptureAsync();


await lowLagCapture.FinishAsync();

第三我正在设置图像源:

Third I am setting the image source:

var softwareBitmap = capturedPhoto.Frame.SoftwareBitmap;
SoftwareBitmap softwareBitmapBGRB = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
await bitmapSource.SetBitmapAsync(softwareBitmapBGRB);

image.Source = bitmapSource;

我如何获得 imageStream?我在 xaml 中使用了 CaptureElement 工具.

How can i get imageStream? I used CaptureElement tool in xaml .

推荐答案

很简单,问题是你想用那个 IRandomAccessStreem 做什么.以下是我认为您需要的一些代码:

It's quite simple, the question is what do you want to do with that IRandomAccessStreem. Below is some code I think you'll need:

public void HandleImageFileOperations(StorageFile file)
{
    if (file != null)
    {
         //converts the StorageFile to IRandomAccessStream
         var stream = await file.OpenAsync(FileAccessMode.Read);
         //creates the stream to an Image just in-case you want to show it
         var image = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
         image.SetSource(stream);

         //creates the image into byte array just in-case you need it to store the image
         byte[] bitmapImageBytes = null;
         var reader = new Windows.Storage.Streams.DataReader(stream.GetInputStreamAt(0));
         bitmapImageBytes = new byte[stream.Size];
         await reader.LoadAsync((uint)stream.Size);
         reader.ReadBytes(bitmapImageBytes);
    }
}

这篇关于如何从 StorageFile 获取 IRandomAccessStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆