I'm trying to use RenderTargetBitmap
to get a screenshot of a Windows Hosted Web App (HWA) window, but I can't seem to instantiate it when handling a request from the Javascript web app running on the HWA app:
public async Task<IBuffer> CaptureScreenshotToFileAsync(uint thumbnailWidth, uint thumbnailHeight, string fileName)
{
// Throws RPC_E_WRONG_THREAD
RenderTargetBitmap screenshotBitmap = new RenderTargetBitmap();
// Assumed that this was a background thread, but...
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
// Surprisingly same thread as above, naturally also throws RPC_E_WRONG_THREAD
RenderTargetBitmap screenshotBitmap = new RenderTargetBitmap();
});
}
In both cases, this is thrown:
System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
at Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap..ctor()
By the way, if I do the following from a background thread in a standard UWP app, it works as expected:
public MainPage()
{
// UI thread
Task.Run(() => {
// Background thread
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
// Back in UI thread.
RenderTargetBitmap targetBitmap = new RenderTargetBitmap(); // doesn't throw.
}
});
}
Can anyone explain:
RenderTargetBitmap
fails in the "UI" thread in a HWA app?User contributions licensed under CC BY-SA 3.0