I've scoured the web for this and not found a solution yet. I have a DispatchTimer in my Universal phone app. On each tick, I want to capture a portion of the screen and save it to JPEG. My code seems very straightforward, and there are no crashes -- it simply never returns from the FlushAsync(). It seems like it must be a deadlock scenario, but I haven't been able to find where the conflict is yet:
using (var ms = new MemoryStream())
{
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(ctrl);
var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
using (var ras = ms.AsRandomAccessStream())
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, ras, propertySet);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied,
(uint) renderTargetBitmap.PixelWidth,
(uint) renderTargetBitmap.PixelHeight,
logicalDpi, logicalDpi,
pixelBuffer.ToArray());
await encoder.FlushAsync();
}
return ms.ToArray();
}
Any help would be great! I've been at this for hours, trying different ways to get it working with no luck.