在WinRT的,我怎么加载图像,然后等待只只要需要为它写入之前加载? [英] 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?

查看:144
本文介绍了在WinRT的,我怎么加载图像,然后等待只只要需要为它写入之前加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WinRT的项目中使用WriteableBitmapEx。我的图像加载到来自用户的图片库中的WriteableBitmap的。但是,我不能然后立即写入到该图像,如果我这样做,它会与图像本身被覆盖(这似乎是它的异步加载图像,然后它的overwritting在它的​​上面我的画)。我不知道如何从这样做,阻止它(我试过在使用的SetSource等候但是这不是一个异步方法)。

我用一个等待Task.Delay(1000)和这样的作品,但似乎哈克,因为1000毫秒可能会或可能没有足够的时间。我想这要等到位图,然后再继续加载。

谁能似乎过顶,我做错了什么,或者建议我怎么能保证做任何处理之前的WriteableBitmap的是从照片库加载?下面是我创建的示例code片断:

 昏暗文件=等待picker.PickSingleFileAsync如果文件只是然后
    退出小组
万一昏暗WBM作为新WriteableBitmap的(1,1)
wbm.SetSource(等待file.OpenAsync(Windows.Storage.FileAccessMode.Read))如果我没有这个,的的DrawLine不会显示出来,如果​​我这样做,会的。
等待Task.Delay(1000)wbm.DrawLine(1,1,255,255,Colors.Green)
wbm.Invalidate()ImageMain.Source = WBM


解决方案

使用openAsync只返回意味着流可用,而不是数据实际上从中读取。好像,你会因此要同时打开+先读,然后你就可以了。

由于菲利普指出,ReadAsync会要求您创建并在缓冲区传递第一,我已经更新了下面的代码片段使用的 中的DataReader流实际加载到字节数组做OpenReadAsync得到IRandomAccessStream后。

  VAR randomAccessStream =等待file.OpenReadAsync();VAR的DataReader =新的DataReader(randomAccessStream);
等待dataReader.LoadAsync(randomAccessStream.Size);字节[] imageBytes;
dataReader.ReadBytes(出imageBytes);wbm.SetSource(新的MemoryStream(imageBytes));

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 image, if I do, it will be overwritten with the image itself (it seems like it's Async loading the image and then it's overwritting my drawing on top of it). I don't know how to stop it from doing that (I tried using Await on the SetSource but that's not an Async method).

I've used an "Await Task.Delay(1000)" and that works, but it seems hacky because 1000ms may or may not be enough time.. I would like it to wait until the bitmap is loaded before proceeding.

Can anyone seem off the top what I'm doing wrong or suggest how I can ensure the WriteableBitmap is loaded from the pictures library before doing any processing? Here's the example code snippet I created:

Dim file = Await picker.PickSingleFileAsync

If file Is Nothing Then
    Exit Sub
End If

Dim wbm As New WriteableBitmap(1, 1)
wbm.SetSource(Await file.OpenAsync(Windows.Storage.FileAccessMode.Read))

' If I don't have this, the DrawLine will not show up, if I do, it will.
Await Task.Delay(1000)

wbm.DrawLine(1, 1, 255, 255, Colors.Green)
wbm.Invalidate()

ImageMain.Source = wbm

解决方案

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

这篇关于在WinRT的,我怎么加载图像,然后等待只只要需要为它写入之前加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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