0

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.

Romasz
  • 29,662
  • 13
  • 79
  • 154
A. V.
  • 61
  • 7
  • I wonder if your issue is that the pclRenderTargetBitmap class itself is going out of scope. If this is in a function that is called, it will return without blocking on the async operation. Then pclRenderTargetBitmap would go out of scope. To verify, you could try making pclRenderTargetBitmap a member of 'this' which would make its lifetime extend past the function in question. – Andy Rich Feb 03 '16 at 20:58
  • This was indeed the problem ! Thanks a lot for your answer. And for your information, this can also be solved juste by passing the lambda parameters by value instead of by reference :auto pclRenderTargetBitmap = ref new Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap; Concurrency::create_task(pclRenderTargetBitmap->RenderAsync(pclElem,100,100)).then([=]() { // handling code here }); – A. V. Feb 09 '16 at 10:48

1 Answers1

0

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
});
A. V.
  • 61
  • 7