0

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?

PutraKg
  • 2,226
  • 3
  • 31
  • 60
  • Why is the downvote? Is this not a programming question? – PutraKg Jul 14 '15 at 16:44
  • Nevermind. I figured it out ;) – PutraKg Jul 14 '15 at 16:54
  • 1
    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 Jul 14 '15 at 18:56

2 Answers2

2

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;
Andrii Krupka
  • 4,276
  • 3
  • 20
  • 41
1

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;
Isdj
  • 1,835
  • 1
  • 18
  • 36