WgcSharp 1.1.1
dotnet add package WgcSharp --version 1.1.1
NuGet\Install-Package WgcSharp -Version 1.1.1
<PackageReference Include="WgcSharp" Version="1.1.1" />
<PackageVersion Include="WgcSharp" Version="1.1.1" />
<PackageReference Include="WgcSharp" />
paket add WgcSharp --version 1.1.1
#r "nuget: WgcSharp, 1.1.1"
#:package WgcSharp@1.1.1
#addin nuget:?package=WgcSharp&version=1.1.1
#tool nuget:?package=WgcSharp&version=1.1.1
WgcSharp
Capture any window by HWND — including occluded, minimized, and DirectX/OpenGL game windows.
Uses Windows.Graphics.Capture (WGC) as primary capture method with DXGI Desktop Duplication fallback. Handles all the COM/WinRT interop complexity so you don't have to.
Why?
PrintWindowreturns black for DirectX/OpenGL game windowsBitBlt/CopyFromScreencan't capture hardware-accelerated content- DXGI Desktop Duplication only captures visible (non-occluded) windows
- Using
Windows.Graphics.Capturedirectly from C# requires painful COM interop with CsWinRT, HSTRING marshaling, and ComWrappers registration — and CsWinRT'sComWrappersconflicts with WPF/.NET 9+ apps
WgcSharp wraps all of this into a single method call. The WGC backend uses raw COM vtable calls internally — no CsWinRT dependency, no ComWrappers conflicts.
Install
dotnet add package WgcSharp
Usage
using WgcSharp;
// Capture any window by its handle
var bitmap = WindowCapture.CaptureWindow(hwnd);
bitmap?.Save("screenshot.png");
Choose a strategy
// WGC only — works for occluded/minimized/DirectX windows (Win10 1803+)
var bitmap = WindowCapture.CaptureWindow(hwnd, CaptureStrategy.WgcOnly);
// DXGI only — captures visible windows via Desktop Duplication
var bitmap = WindowCapture.CaptureWindow(hwnd, CaptureStrategy.DxgiOnly);
// Auto (default) — tries WGC first, falls back to DXGI
var bitmap = WindowCapture.CaptureWindow(hwnd, CaptureStrategy.Auto);
Logging
Pass an optional ILogger to get detailed capture diagnostics:
using Microsoft.Extensions.Logging;
var loggerFactory = LoggerFactory.Create(b => b.AddConsole());
var logger = loggerFactory.CreateLogger("WgcSharp");
var bitmap = WindowCapture.CaptureWindow(hwnd, CaptureStrategy.Auto, timeoutMs: 2000, logger: logger);
Full example
using System.Diagnostics;
using System.Drawing.Imaging;
using WgcSharp;
var process = Process.GetProcessesByName("notepad").First();
var bitmap = WindowCapture.CaptureWindow(process.MainWindowHandle);
bitmap?.Save("notepad.png", ImageFormat.Png);
How it works
WGC Backend (primary)
- Creates a D3D11 device and wraps it as a WinRT
IDirect3DDevice - Creates a
GraphicsCaptureItemfor the target HWND viaIGraphicsCaptureItemInterop - Sets up a free-threaded
Direct3D11CaptureFramePooland capture session - Skips the first frame (often blank) and captures the second frame
- Extracts the D3D11 texture via
IDirect3DDxgiInterfaceAccess - Copies to a staging texture and reads pixels into a
System.Drawing.Bitmap
All WinRT interaction uses raw COM vtable calls — no CsWinRT, no ComWrappers registration, no conflicts with WPF or other frameworks.
DXGI Backend (fallback)
- Creates a D3D11 device and finds the DXGI output for the target window's monitor
- Uses
IDXGIOutput1.DuplicateOutputto capture the full desktop frame - Crops to the target window's screen coordinates (via
DwmGetWindowAttribute)
API
// Simple — uses Auto strategy, 2s timeout
Bitmap? CaptureWindow(IntPtr hwnd, int timeoutMs = 2000)
// Full control
Bitmap? CaptureWindow(IntPtr hwnd, CaptureStrategy strategy, int timeoutMs = 2000, ILogger? logger = null)
Requirements
- Windows 10 version 1803+ (build 17134) for WGC
- .NET 9.0+ or .NET 10.0+ (Windows TFM)
- Hardware GPU (D3D11)
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0-windows10.0.19041 is compatible. net10.0-windows was computed. net10.0-windows10.0.19041 is compatible. |
-
net10.0-windows10.0.19041
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- System.Drawing.Common (>= 9.0.5)
- Vortice.Direct3D11 (>= 3.8.1)
-
net9.0-windows10.0.19041
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- System.Drawing.Common (>= 9.0.5)
- Vortice.Direct3D11 (>= 3.8.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.