gpt4 book ai didi

windows-runtime - 在 WinRT 中,我如何加载一个图像,然后只等待它加载所需的时间再写入它?

转载 作者:行者123 更新时间:2023-12-04 06:51:26 24 4
gpt4 key购买 nike

我在 WinRT 项目中使用 WriteableBitmapEx。我从用户图片库将图像加载到 WriteableBitmap 中。但是,我不能立即写入该图像,如果我这样做,它将被图像本身覆盖(看起来它是异步加载图像然后它覆盖我在它上面的绘图)。我不知道如何阻止它这样做(我尝试在 SetSource 上使用 Await 但这不是异步方法)。

我使用了“Await Task.Delay(1000)”并且它有效,但它看起来很老套,因为 1000 毫秒可能不够也可能不够。我希望它等到位图加载后再继续。

谁能看出我做错了什么,或者建议我如何确保在进行任何处理之前从图片库加载 WriteableBitmap?这是我创建的示例代码片段:

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

最佳答案

此方法从应用程序的内容中加载图像,对其进行解码并返回一个随时可用的 WriteableBitmap。取自WriteableBitmapEx library :

/// <summary>
/// Loads an image from the applications content and fills this WriteableBitmap with it.
/// </summary>
/// <param name="bmp">The WriteableBitmap.</param>
/// <param name="uri">The URI to the content file.</param>
/// <returns>The WriteableBitmap that was passed as parameter.</returns>
public static async Task<WriteableBitmap> FromContent(this WriteableBitmap bmp, Uri uri)
{
// Decode pixel data
var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
var decoder = await BitmapDecoder.CreateAsync(await file.OpenAsync(FileAccessMode.Read));
var transform = new global::Windows.Graphics.Imaging.BitmapTransform();
var pixelData = await decoder.GetPixelDataAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb);
var pixels = pixelData.DetachPixelData();

// Copy to WriteableBitmap
bmp = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
using (var bmpStream = bmp.PixelBuffer.AsStream())
{
bmpStream.Seek(0, SeekOrigin.Begin);
bmpStream.Write(pixels, 0, (int)bmpStream.Length);
return bmp;
}
}

顺便说一句,WriteableBitmapEx 现在正式支持 WinRT。 ;) http://kodierer.blogspot.de/2012/05/one-bitmap-to-rule-them-all.html

关于windows-runtime - 在 WinRT 中,我如何加载一个图像,然后只等待它加载所需的时间再写入它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10197170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com