【问题标题】:How to get IRandomAccessStream from StorageFile如何从 StorageFile 获取 IRandomAccessStream
【发布时间】:2017-12-10 23:45:08
【问题描述】:

首先我声明

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

【问题讨论】:

    标签: c# uwp


    【解决方案1】:

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

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多