Search code examples
windows-runtimewin-universal-appc++-cxrendertargetbitmap

How should RenderTargetBitmap be used?


In a Windows Universal App (WinRT) application, I am trying to capture a bitmap image of the current page (or a portion of it).

A google search indicated that I should use the class Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap (more specifically its method RenderAsync() ) to capture the screen.

In a small sample application, I thus added this code (C++) :

auto pclRenderTargetBitmap = ref new Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap;
Concurrency::create_task(pclRenderTargetBitmap->RenderAsync(pclElem,100,100)).then([&]() {
  // handling code here
});

(pclElem is a FrameworkElement, more specifically a canvas, and is not null)

When I execute this code, the task is indeed created, but the lambda in the "then" is never called. It's as if RenderAsync() never terminates.

Does anyone have any experience with using this function in C++ ? What am I missing ?

Thanks for your answers.


Solution

  • Thanks to Andy Rich for his answer. The problem was that the pclRenderTargetBitmap was going out of scope. This can be solved by passing the lambda parameters by value :

    auto pclRenderTargetBitmap = ref new  Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap;
    Concurrency::create_task(pclRenderTargetBitmap->RenderAsync(pclElem,100,100)).then([=]() {
          // handling code here
    });