question

Aqib-3263 avatar image
0 Votes"
Aqib-3263 asked ·

GraphicsCapturePicker doesnt work in WPF .NET 5 project.

         GraphicsCapturePicker gPicker = new GraphicsCapturePicker();
         var item = await gPicker.PickSingleItemAsync();

https://github.com/dotnet/core/issues/5670

windows-uwpwindows-wpf
· 3
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Aqib-3263 Did you use the IsSupported method in the GraphicsCaptureSession class to determine if UWP screen capture is supported? Code is like below:

 if (!GraphicsCaptureSession.IsSupported())
     {
         // Hide the capture UI if screen capture is not supported.
            
     }
0 Votes 0 ·

The problem here is something else.

It is working for .Net Framework but it's not working for .NET 5.

System.InvalidCastException: 'Invalid cast from 'Windows.Graphics.Capture.GraphicsCapturePicker' to 'WpfApp1.IInitializeWithWindow'.'

Exception thrown: 'System.InvalidCastException' in System.Private.CoreLib.dll
Exception thrown: 'System.InvalidCastException' in System.Private.CoreLib.dll
An exception of type 'System.InvalidCastException' occurred in System.Private.CoreLib.dll but was not handled in user code
Invalid cast from 'Windows.Graphics.Capture.GraphicsCapturePicker' to 'WpfApp1.IInitializeWithWindow'.

0 Votes 0 ·

WITHOUT IInitializeWithWindow
Exception thrown: 'System.Runtime.InteropServices.COMException' in System.Private.CoreLib.dll
An exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Private.CoreLib.dll but was not handled in user code
The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD))

0 Votes 0 ·
Aqib-3263 avatar image
0 Votes"
Aqib-3263 answered ·

This doesn't Work as it uses WinRT code/libraries which is not directly supported by .NET 5.
This is a simple solution to this issue.


         var interopWindow = new WindowInteropHelper(this);
         hwnd = interopWindow.Handle;
         var picker = new GraphicsCapturePicker();
         var window = picker.As<IInitializeWithWindow>();
         window.Initialize(hwnd);
         var item = await picker.PickSingleItemAsync();
· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DaisyTian-MSFT avatar image
0 Votes"
DaisyTian-MSFT answered ·

Add Composition.WindowsRuntimeHelpers.dll to your project , you can download it from here. I used below code in my .Net 5 WPF project , and it runs with no error.

 public partial class MainWindow : Window
     {
         private IntPtr hwnd;
         public MainWindow()
         {
             InitializeComponent();
         }
    
         private async void MyBtn_Click(object sender, RoutedEventArgs e)
         {
             await StartPickerCaptureAsync();
         }
    
         private async Task StartPickerCaptureAsync()
         {
             var picker = new GraphicsCapturePicker();
             picker.SetWindow(hwnd);
             GraphicsCaptureItem item = await picker.PickSingleItemAsync();
    
             if (item != null)
             {
                 //sample.StartCaptureFromItem(item);
             }
         }
    
         private void Window_Loaded(object sender, RoutedEventArgs e)
         {
             var interopWindow = new WindowInteropHelper(this);
             hwnd = interopWindow.Handle;
         }
     }


· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.