Search code examples
c#uwpscreen-capture

Microsoft Screen Capture – C# UWP application


On the following Microsoft Screen Capture link it shows example C# UWP app code. https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/screen-capture

I can’t compile the Putting it all together example code at the bottom of the page.

  • I’ve created a new C# Blank App (Universal Windows) project in Visual Studio 2022 Community edition.
  • Copied and pasted the ‘Putting it all together’ example code into the code view of the MainPage.xaml file
  • Viewed the Package.appxmanifest file in the designer and selected the ‘Graphics Capture’ option on the Capabilities tab (as per the instructions on the Screen Capture link)

This showed the Microsoft.Graphics namespace was missing from the project when I tried to compile it. So I searched for Microsoft.Graphics in the NuGet Manager and installed Microsoft.Graphics.Win2D package (v1.0.5.1) by Microsoft.

This fixed most of the compile errors, but it showed there were still errors. One of them being the error on the following line:

_compositionGraphicsDevice = CanvasComposition.CreateCompositionGraphicsDevice(
    Window.Current.Compositor,
    _canvasDevice);

The compiler outputted:

Argument 1: cannot convert from 'Windows.UI.Composition.Compositor' to 'Microsoft.UI.Composition.Compositor'

Window.Current.Compositor returns Windows.UI.Composition.Compositor type whereas the CanvasComposition.CreateCompositionGraphicsDevice method is expecting Microsoft.UI.Composition.Compositor type as the 1st argument.

Can someone please tell me what I need to reference or do to get the example code to compile? Thank you


Solution

  • You need to use Win2D.uwp in UWP. The Microsoft.Graphics.Win2D you installed is for use with WinUI3 and Project Reunion.

    Win2D currently has two packages available:

    • Win2D.uwp

    For use with the WinUI2 (Universal Windows Platform only)

    • Microsoft.Graphics.Win2D

    For use with WinUI3 and Project Reunion