0
votes

I know how to do this in Silverlight but I can't find enough information on how to do this in WinRT.

I read that WinRT XAML Toolkit may be able to help but the exact component, WinRT XAML Toolkit - Composition does not seem to be compatible with Windows Universal App. I am currently developing for Windows Phone 8.1 part.

WinRT XAML Toolkit for Windows Phone 8.1 does not seem to have something like WriteableBitmap.Render method either.

I've also read that the blit method in WriteableBitmapEx may be able to help but I couldn't find any example on how to achieve this.

Any deas?

2
Why is the downvote? Is this not a programming question? - PutraKg
Nevermind. I figured it out ;) - PutraKg
Downvotes with no comment are rude, I agree. Just a comment for the Toolkit - I intentionally reduced support and visibility of the composition library since 8.1 added RenderTargetBitmap which addresses most of the same scenarios. I left it in the toolkit sources though as an inspiration and in case at any point it turns out that RenderTargetBitmap doesn't support some important scenario. For example - it could be modified to be used to add GPU-powered pixel shader effects to your UI if needed. - Filip Skakun

2 Answers

2
votes

You can use RenderTargetBitmap to create image from your UIElement which contains in VisualTree.

  var renderTargetBitmap = new RenderTargetBitmap();
  await renderTargetBitmap.RenderAsync(uielement);

  var pixels = await renderTargetBitmap.GetPixelsAsync();

  var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
  var encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
  encoder.SetPixelData(
      BitmapPixelFormat.Bgra8,
      BitmapAlphaMode.Ignore,
      (uint)renderTargetBitmap.PixelWidth,
      (uint)renderTargetBitmap.PixelHeight,
      logicalDpi,
      logicalDpi,
      pixels.ToArray());

  await encoder.FlushAsync();

  return renderTargetBitmap;
1
votes

Simpler to use if you want to display it, in order to save you must do as the answer above:

RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(RenderedGrid);
RenderedImage.Source = renderTargetBitmap;