• Stars
    star
    288
  • Rank 143,177 (Top 3 %)
  • Language
    C++
  • License
    MIT License
  • Created over 6 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple sample using the Windows.Graphics.Capture APIs in a Win32 application.

Win32CaptureSample

A simple sample using the Windows.Graphics.Capture APIs in a Win32 application.

Table of Contents

Requirements

This sample requires the Windows 11 SDK (10.0.22000.194) and Visual Studio 2022 to compile. Neither are required to run the sample once you have a binary. The minimum verison of Windows 10 required to run the sample is build 17134.

Win32 vs UWP

This sample demonstrates using the Windows.Graphics.Capture APIs in a Win32 application. For the most part, the usage is the same, except for a few tweaks:

HWND or HMONITOR based capture

Win32 applications have access to the IGraphicsCaptureItemInterop interface. This interface can be found by QIing for it on the GraphicsCaptureItem factory:

#include <winrt/Windows.Graphics.Capture.h>
#include <windows.graphics.capture.interop.h>
#include <windows.graphics.capture.h>

namespace winrt
{
    using namespace Windows::Graphics::Capture;
}

// Obtaining the factory
auto interopFactory = winrt::get_activation_factory<
    winrt::GraphicsCaptureItem, 
    IGraphicsCaptureItemInterop>();

winrt::GraphicsCaptureItem item{ nullptr };

// Creating a GraphicsCaptureItem from a HWND
winrt::check_hresult(interopFactory->CreateForWindow(
    hwnd, 
    winrt::guid_of<winrt::GraphicsCaptureItem>(), 
    winrt::put_abi(item)));

// Creating a GraphicsCaptureItem from a HMONITOR
winrt::check_hresult(interopFactory->CreateForMonitor(
    hmon, 
    winrt::guid_of<winrt::GraphicsCaptureItem>(), 
    winrt::put_abi(item)));

This sample makes uses a collection of common header files which contains helpers for GraphicsCaptureItem creation.

Using the GraphicsCapturePicker

Win32 applications may also use the GraphicsCapturePicker to obtain a GraphicsCaptureItem, which asks the user to do the selection. Like other pickers, the GraphicsCapturePicker won't be able to infer your window in a Win32 application. You'll need to QI for the IInitializeWithWindow interface and provide your window's HWND.

#include <winrt/Windows.Graphics.Capture.h>
#include <shobjidl_core.h>

namespace winrt
{
    using namespace Windows::Graphics::Capture;
}

auto picker = winrt::GraphicsCapturePicker();
auto initializeWithWindow = picker.as<IInitializeWithWindow>();
winrt::check_hresult(initializeWithWindow->Initialize(hwnd));
// The picker is now ready for use!

Create vs CreateFreeThreaded

When a Direct3D11CaptureFramePool is created using Create, you're required to have a DispatcherQueue for the current thread and for that thread to be pumping messages. When created with Create, the FrameArrived event will fire on the same thread that created the frame pool.

When created with CreateFreeThreaded, there is no DispatcherQueue requirement. However, the FrameArrived event will fire on the frame pool's internal thread. This means that the callback you provide must be agile (this is usually handled by the language projection).

Points of interest

The code is organized into a couple of classes:

  • App.h/cpp handles the basic logic of the sample, as well as setting up the visual tree to present the capture preview.
  • SampleWindow.h/cpp handles the main window and the controls.
  • SimpleCapture.h/cpp handles the basics of using the Windows.Graphics.Capture API given a GraphicsCaptureItem. It starts the capture and copies each frame to a swap chain that is shown on the main window.
  • CaptureSnapshot.h/cpp shows how to take a snapshot with the Windows.Graphics.Capture API. The current version uses coroutines, but you could synchronously wait as well using the same events. Just remember to create your frame pool with CreateFreeThreaded so you don't deadlock!

More Repositories

1

minesweeper-rs

A port of robmikh/Minesweeper using windows-rs.
Rust
788
star
2

SimpleRecorder

A simple screen recorder using both the Windows.Graphics.Capture and Windows.Media.Transcoding APIs.
C#
220
star
3

Minesweeper

A C++ implementation of Minesweeper using Windows.UI.Composition.
C++
52
star
4

screenshot-rs

Take screenshots using Windows.Graphics.Capture and windows-rs.
Rust
51
star
5

compositionsurfacefactory

An image loader to use with the Windows.UI.Composition API based on Win2D and written with C++/WinRT.
C++
35
star
6

CaptureGifEncoder

A simple screen gif encoder using Windows.Graphics.Capture and WIC.
C++
23
star
7

SoHConfig

A configuration utility for the Ship of Harkinian.
C#
23
star
8

displayrecorder

A utility to record displays.
Rust
16
star
9

Solitaire

A C++ implementation of Solitaire using Windows.UI.Composition.
C++
15
star
10

ImageViewer

A tool for viewing image files and bitmaps on Windows 10.
C#
14
star
11

BasicCaptureSample

Basic sample using the Windows.Graphics.Capture API along with Win2D.
C#
13
star
12

audiovisualization

UWP audio visulization sample. Work in progress.
C++
12
star
13

WPFCaptureSample

C#
11
star
14

blog.samples

A place to find the samples I create on my blog.
C#
10
star
15

CaptureVideoSample

A sample that records video using the Windows.Graphics.Capture and Windows.Media.Transcoding APIs.
C++
8
star
16

paint

An attempt to replicate the functionality of paint using Win2D in C#.
C#
7
star
17

robmikh.common

A collection of headers I commonly use. Not meant to be stable.
C++
6
star
18

windows-template-rs

A basic template to get started using windows-rs.
Rust
6
star
19

goldsrc-asset-viewer

Rust
6
star
20

CaptureAdHocTest

An assortment of ad-hoc tests for the Windows.Graphics.Capture API.
C++
6
star
21

UWPCaptureGifEncoder

A simple screen gif encoder using Windows.Graphics.Capture and Windows.Graphics.Imaging.
C#
5
star
22

comptextdemo-rs

A short demo of using DirectWrite along with Windows.UI.Composition.
Rust
5
star
23

svg-cards

A set of playing cards as SVG images.
4
star
24

ManagedScreenshotDemo

C#
4
star
25

WinUI3CaptureSample

A simple sample on how to use Windows.Graphics.Capture with WinUI3.
C#
4
star
26

WinUI3Win2DSwapChain

A sample showing the use of Windows.Graphics.Capture, Win2D, and WinUI3.
C#
4
star
27

giffun

An experimental gif encoder for Windows 10.
Rust
3
star
28

WinRTInteropTools

A collection of tools used for prototyping.
C++
3
star
29

ScreenshotFun

C++
3
star
30

CompositionTetris

An unofficial C# implementation of Tetris using the Composition API.
C#
3
star
31

solitaire-rs

A port of robmikh/Solitaire using windows-rs.
Rust
3
star
32

nuget_cpp

A tool that assists in packaging C++/WinRT components for NuGet.
Rust
3
star
33

wgc-rust-demo

A simple demo that takes a screenshot using Windows.Graphics.Capture and Rust/WinRT.
Rust
3
star
34

ScreenshotSample

A sample that shows how to take and save screenshots using Windows.Graphics.Capture.
C++
2
star
35

CaptureRateTest

A program to test the performance of Windows.Graphics.Capture while starving the DWM of buffers.
C++
2
star
36

HelloTriangle11

A simple D3D11 sample.
C++
2
star
37

HelloTriangle12

A simple D3D12 sample.
C
2
star
38

CompositionTextDemo

A short demo of using DirectWrite along with Windows.UI.Composition.
C++
2
star
39

showorder

A utility to help order video files from TV shows using subtitles.
Rust
2
star
40

compsvg

An experimental svg to CompositionShape converter.
Rust
2
star
41

CompositionImageDemo

A minimal sample showing how to load an image into a surface and respond to device lost events using Windows.UI.Composition and Windows.Graphics.Imaging.
C++
2
star
42

chartfun

An experiment in rendering charts using D2D and Windows.UI.Composition.
Rust
2
star
43

robmikh.github.io

A development blog about all things UWP and Composition.
SCSS
1
star
44

gifconvert

A utility to convert images to gifs.
Rust
1
star
45

Win2DWpfTest

C#
1
star
46

GifExplorer

A tool to view detailed information from gif files.
C#
1
star
47

D3D11StereoTest

A test app to exercise D3D11 stereo functionality.
C++
1
star
48

TextureConverterSample

A sample that shows how to use D2D and D3D11 to convert between different texture dimensions and formats.
C++
1
star
49

UWPCaptureSample

A simple sample using the Windows.Graphics.Capture APIs in a UWP application.
C#
1
star
50

helloDML-rs

A port of the HelloDirectML sample to Rust.
Rust
1
star
51

wgctest

A set of automated tests for Windows.Graphics.Capture.
Rust
1
star
52

vidcapview

A utility to view video capture devices.
Rust
1
star
53

squash_the_creeps-rs

A port of GDQuest / godot-3d-dodge-the-creeps to Rust
Rust
1
star
54

WICAnimatedGifDecode

Port of the original WIC Animated Gif Sample from Microsoft.
C++
1
star
55

dwmdumper

A utility to take a memory dump of DWM.exe using a hotkey.
Rust
1
star
56

Robmikh.Interop.Composition

A collection of interop bits for Windows.UI.Composition.
C++
1
star
57

WGCOpenCVInterop

A small demo on how to interop Windows.Graphics.Capture (really Direct3D11) with OpenCV.
C++
1
star