question

Aqib-3263 avatar image
0 Votes"
Aqib-3263 asked aqibbanday answered

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
5 |1600 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();
5 |1600 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-1203 avatar image
0 Votes"
DaisyTian-1203 answered Aqib-3263 converted comment to answer

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;
         }
     }


5 |1600 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.

WillTingley avatar image
0 Votes"
WillTingley answered

So I've been struggling with this problem for about a week now, and I finally figured it out today. I want to post my solution to the problem in hopes that it helps anyone who stumbles across this.

As the question asks, it's for a WPF .NET 5.0 project. It's pretty short, but there are a few things to note:

In your .csproj file, your TargetFramework should be set to 10.0.17763.0 or higher (anything higher than 8.0). This will look like <TargetFramework>net5.0-windows10.0.20348.0</TargetFramework>, which in my case is the latest framework available. If you don't have this, the Windows.Graphics.Capture and WinRT.Interop namespaces will not be available to you. These namespaces have the classes we need.

It's really only a slight fix from the answer @DaisyTian-1203 gave, but you don't need to fumble around with the Composition.WindowsRuntimeHelpers.dll. Hope this helps someone out there!

Code in full:

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using Windows.Graphics.Capture;
using WinRT.Interop;

namespace MyProject
{
    public partial class MainWindow : Window
    {
        IntPtr Hwnd { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            Loaded += OnLoad;
        }

        async void OnLoad(object sender, RoutedEventArgs e)
        {
            Trace.WriteLine("Application Loaded");
            await StartPickerCaptureAsync();
        }

        async Task StartPickerCaptureAsync()
        {
            if (!GraphicsCaptureSession.IsSupported())
            {
                Trace.WriteLine("No Capture Support");
            }

            var interopWindow = new WindowInteropHelper(this);
            Hwnd = interopWindow.Handle;

            var picker = new GraphicsCapturePicker();
            InitializeWithWindow.Initialize(picker, Hwnd);

            var item = await picker.PickSingleItemAsync();
        }
    }
}

5 |1600 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.

aqibbanday avatar image
0 Votes"
aqibbanday answered

I have already posted the answer to this question myself.

5 |1600 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.