WriteableBitmap crashes on constructor when run from opentap UI test launcher editor

Hi

I am trying to use WriteableBitmap in one of my tests. However, when launched via opentap UI Editor test launched, WriteableBitmap crashes on constructor with the following exception:

System.Exception
HResult=0x8001010E
Message=The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

WriteableBitmap is object type from modern windows APIs (Universal app) that uses UI elements so it needs a STA thread to work properly. I use WriteableBitmap via Microsoft.Windows.SDK.Contracts which is a Microsoft’s API that allows regular .net applications to run modern API (Universal app) objects as if it was a native c# .net core or framework object.

WriteableBitmap works fine a legacy inhouse .net framework WPF test launcher UI.
Is there any way WriteableBitmap can work when launched with the opentap UI test launcher editor?

image

Thanks

3 Likes

Hi @joaquin.a.valdez and welcome to the forum!

According to MS Documentation only the UI thread is allowed to make modifications to the bitmap.

I haven’t used UWP, but it seems you’d need to do something like:

 Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
   // modify your bitmap here.
});

However, this code would only be able to run inside a UWP application, so maybe there is a better way. What are you using the bitmap for?

1 Like

That’s correct. In the legacy test runner, I invoked the dispatcher when using this structure. The propose for the structure ion my code is not poke around a UI element but to use it as an intermediary between a SoftwareBitmap coming from the camera (that can be on multiple different formats) and moving it into an bgr array (to then later convert to an open CV image to do image processing work). Unfortunately, windows tied some components of WriteableBitmap to the UI so the object itself requires a UI tread.

Yeah, I can find another way to do this conversion, but I was wondering if there was any way in open tap to invoke the UI tread (or STA tread) before changing the code.

        using (SoftwareBitmap bgrSoftwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8))
        {
            WriteableBitmap bgrWriteBitmap = new WriteableBitmap(bgrSoftwareBitmap.PixelWidth, bgrSoftwareBitmap.PixelHeight);
            bgrSoftwareBitmap.CopyToBuffer(bgrWriteBitmap.PixelBuffer);

We don’t have a way to do this in OpenTAP. I think that depends on which UI framework you are using.

Anyway, couldn’t you just keep it as a SoftwareBitmap and only using WriteableBitmap for rendering?

2 Likes

I just rewrote the code using a different path to get the image to be on open CV.

Thanks.

1 Like