如何从StorageFile获取IRandomAccessStream

时间:2017-12-09 04:40:08

标签: c# uwp

首先我宣布了

private MediaCapture _mediaCapture;
StorageFile capturedPhoto;
IRandomAccessStream imageStream;

第二我正在捕捉

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

var capturedPhoto = await lowLagCapture.CaptureAsync();


await lowLagCapture.FinishAsync();

第三我正在设置图像源:

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工具。

1 个答案:

答案 0 :(得分:0)

这很简单,问题是你想用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); } } 做什么。下面是我认为你需要的一些代码:

if (stack.canDoOperation()) {
    switch(calculation[i]){
        case "*": times(); break;
        case "+": sum();   break;
} else {
    operationFailed = true;
}