In WinRT, how do I load an image and then wait only as long as is needed for it to load before writing to it?

后端 未结 3 1272
有刺的猬
有刺的猬 2021-01-15 13:51

I\'m using WriteableBitmapEx in a WinRT project. I load an image into a WriteableBitmap from the users Picture library. However, I cannot then immediately write to that im

3条回答
  •  一整个雨季
    2021-01-15 14:11

    The OpenAsync returning just means the stream is available, not that the data is actually read from it. It seems like you would therefore want to both open + read first and then you would be fine.

    Since Filip pointed out that ReadAsync would require you to create and pass in a buffer first, I've updated the below snippet to use DataReader to actually load the stream into the byte array after doing OpenReadAsync to get the IRandomAccessStream.

    var randomAccessStream = await file.OpenReadAsync();
    
    var dataReader = new DataReader(randomAccessStream);
    await dataReader.LoadAsync(randomAccessStream.Size);
    
    byte[] imageBytes;
    dataReader.ReadBytes(out imageBytes);
    
    wbm.SetSource(new MemoryStream(imageBytes));
    

提交回复
热议问题