question

HankMody-6589 avatar image
0 Votes"
HankMody-6589 asked HankMody-6589 commented

About GraphicsCaptureItem:How to get the DisplayId(WindowId) in TryCreateFromDisplayId(DisplayId)(TryCreateFromWindowId(WindowId))?

I want to use TryCreateFromDisplayId(DisplayId) to get the GraphicsCaptureItem,I can't find any way to get the DisplayId or WindowId!Please show me the way....

Simple UWP code (I tried to use handle to get GraphicsCaptureItem):
CS code:

 using System;
 using System.Globalization;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices.WindowsRuntime;
 using Windows.Graphics.Capture;
 using Windows.UI.Xaml.Controls;

 namespace App1
 {
       public sealed partial class MainPage : Page
       {
            private bool Set=false;
            public MainPage()
            {
                 this.InitializeComponent();
            }
           private void TextBlock_SelectionChanged(object sender, Windows.UI.Xaml.RoutedEventArgs e)
            {
              if (!Set)  HandleInput.Text = "";
             Set = true;
             Enter.IsEnabled = true;
           }
           private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
          {
           Set = false;
           var hd = int.Parse(HandleInput.Text, NumberStyles.HexNumber);
           IntPtr hwd = new IntPtr(hd);
           var item = CaptureItemHelper.CreateItemForWindow(hwd);
           ItemList.ItemsSource = item.ToString();
           Enter.IsEnabled=false;
             }
           }
 public static class CaptureItemHelper
 {
     static Guid GraphicsCaptureItemGuid = new Guid("79C3F95B-31F7-4EC2-A464-632EF5D30760");
     [ComImport]
     [Guid("3628E81B-3CAC-4C60-B7F4-23CE0E0C3356")]
     [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
     [ComVisible(true)]
     interface IGraphicsCaptureItemInterop
     {
         IntPtr CreateForWindow(
             [In] IntPtr window,
             [In] ref Guid iid);

         IntPtr CreateForMonitor(
             [In] IntPtr monitor,
             [In] ref Guid iid);
     }
     public static GraphicsCaptureItem CreateItemForWindow(IntPtr hwnd)
     {
         var factory = WindowsRuntimeMarshal.GetActivationFactory(typeof(GraphicsCaptureItem));
         var interop = (IGraphicsCaptureItemInterop)factory;
         //var temp = typeof(GraphicsCaptureItem);           
         var itemPointer = interop.CreateForWindow(hwnd, GraphicsCaptureItemGuid);
         var item = Marshal.GetObjectForIUnknown(itemPointer) as GraphicsCaptureItem;
         Marshal.Release(itemPointer);
         return item;
        }
     }
 }

XAML code:

 <Page
 x:Class="App1.MainPage"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:local="using:App1"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d"
 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

  <Grid>
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="40">
         <TextBox x:Name="HandleInput" FontSize="20" Text="Please enter a hexadecimal handle!" 
                        SelectionChanged="TextBlock_SelectionChanged" Margin="0,0,40,0"/>
         <Button  x:Name="Enter" FontSize="20" Content="Enter" IsEnabled="False" Click="Button_Click" />
         <ListView x:Name="ItemList" Margin="20"  HorizontalAlignment="Center"/>
     </StackPanel>  
  </Grid>
 </Page>


windows-uwp
· 11
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.

Could you please tell me what kind of function do you want to implement?

0 Votes 0 ·

I want to capture the window in real time to detect something. If use GraphicsCapturePicker , some Windows cannot be selected (some Windows do not exist in the selection list). GraphicsCaptureItem also doesn't provide a way to accurately capture the window through the Handle, I wonder if TryCreateFromDisplayId(DisplayId) or TryCreateFromWindowId(WindowId) provides a similar method。But I didn't find a way to get the windows DisplayId or the WindowId.Or are there any method that can be implemented using handle?Thank you for your reply!

0 Votes 0 ·

l'll consult the team about this. There might be some time delay.

0 Votes 0 ·
Show more comments

@HankMody-6589 Sorry for the delay. But after confirmed with the team. What you want to do is not supported yet now. The Capture is in preview some features are not fully slated yet. We'd suggest you submit feedback for this. You could go to this link: GraphicsCaptureSession Class. Scroll to the bottom and click the This Page button in the Feedback part. Then write down what you are trying to achieve and submit it.

0 Votes 0 ·

@ RoyLi-MSFT OK,I will try it.Thanks very much!

0 Votes 0 ·

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered HankMody-6589 edited

Maybe you can try with IGraphicsCaptureItemInterop::CreateForWindow as explained at New Ways to do Screen Capture
(in .NET/Win32 I also use DwmRegisterThumbnail then D3DXSaveSurfaceToFileInMemory to capture windows in real-time)





· 1
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.

I tried to use this sample in uwp. It doesn't work properly and will report all kinds of strange errors!Like:“Method system.reflection.methodbase. get_ IsConstructedGenericMethod cannot be invoked in this context.”.But it works well in WPF!

0 Votes 0 ·