GraphicsCapturePicker doesnt work in WPF .NET 5 project.

Aqib 96 Reputation points
2020-11-27T11:02:30.9+00:00
        GraphicsCapturePicker gPicker = new GraphicsCapturePicker();
        var item = await gPicker.PickSingleItemAsync();

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

Universal Windows Platform (UWP)
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,665 questions
{count} votes

Accepted answer
  1. Aqib 96 Reputation points
    2020-12-03T12:48:32.603+00:00

    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();
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2020-12-03T06:41:47.63+00:00

    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;
            }
        }
    
    0 comments No comments

  2. Will Tingley 1 Reputation point
    2021-10-19T21:28:19.277+00:00

    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();  
               }  
           }  
       }  
    
    0 comments No comments

  3. Aqib Nazir 1 Reputation point
    2021-10-20T04:22:06.437+00:00

    I have already posted the answer to this question myself.

    0 comments No comments