我在 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 库:
/// <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://kodieer.blogspot.de/2012/05/one-bitmap-to-rule-them-all.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)