public static async Task<WriteableBitmap> RenderUIElement(UIElement element)
{
var bitmap = new RenderTargetBitmap();
await bitmap.RenderAsync(element);
var pixelBuffer = await bitmap.GetPixelsAsync();
var pixels = pixelBuffer.ToArray();
var writeableBitmap = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
using (Stream stream = writeableBitmap.PixelBuffer.AsStream())
{
await stream.WriteAsync(pixels, 0, pixels.Length);
}
return writeableBitmap;
}
Since WriteableBitmap
is an ImageSource
you can use it to set the Source property of an Image control either though a Binding or code-behind