Dưới Windows, có 3 “họ” cơ chế chụp màn hình phổ biến nhất – mỗi bạn có ưu/nhược riêng. Tuỳ nhu cầu (độ ổn định, tốc độ, DPI scaling, đa màn hình…) bạn chọn:
GDI+ (BitBlt)
Desktop Duplication API (DXGI, Win10+)
Windows.Graphics.Capture (WinRT, Win10 1903+)
1) GDI+ (BitBlt)
– Ưu: đơn giản, hỗ trợ từ Windows XP → nay;
– Nhược: chậm hơn, không “zero-copy”, dễ gặp hiện tượng giật khung hình cao khi FPS lớn;
– Kịch bản: chụp ảnh tĩnh, không yêu cầu ghi video tốc độ cao.
Ví dụ C# (P/Invoke + BitBlt)
csharp
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
class ScreenCaptureGDI
{
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdcDest, int x, int y, int nWidth, int nHeight,
IntPtr hdcSrc, int xSrc, int ySrc, CopyPixelOperation dwRop);
public static Bitmap CaptureScreen()
{
var screenSize = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size;
var bmp = new Bitmap(screenSize.Width, screenSize.Height, PixelFormat.Format32bppArgb);
using (Graphics gDest = Graphics.FromImage(bmp))
using (Graphics gSrc = Graphics.FromHwnd(IntPtr.Zero))
{
IntPtr hdcDest = gDest.GetHdc();
IntPtr hdcSrc = gSrc.GetHdc();
BitBlt(hdcDest, 0, 0, screenSize.Width, screenSize.Height,
hdcSrc, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
gDest.ReleaseHdc(hdcDest);
gSrc.ReleaseHdc(hdcSrc);
}
return bmp;
}
}
2) Desktop Duplication API (DXGI)
– Ưu: hiệu năng cao, hỗ trợ multi-monitor, độ trễ rất thấp, phù hợp capture video/game;
– Nhược: phức tạp hơn, chỉ dùng được trên Win10 trở lên, cần dùng Direct3D11 + interop C# (SharpDX/Vortice.Windows,…).
Workflow cơ bản
Tạo D3D11 Device & Context.
Lấy IDXGIOutputDuplication từ IDXGIOutput1.
Gọi AcquireNextFrame để lấy frame mã hóa sẵn.
Copy resource sang staging texture rồi map → đọc CPU.
ReleaseFrame.
Thư viện hỗ trợ (C#)
SharpDX (đang ít bảo trì)
Vortice.Windows (đang active)
csharp
// (Demo ngắn gọn, bạn nên tham khảo repo chính thức)
using Vortice.DXGI;
using Vortice.Direct3D11;
// 1) D3D11Device = D3D11CreateDevice(...)
// 2) swapChain = ...
// 3) var output1 = dxgiOutput.QueryInterface<Output1>();
// 4) duplicatedOutput = output1.DuplicateOutput(d3dDevice);
int timeout = 500;
if (duplicatedOutput.TryAcquireNextFrame(timeout, out var frameInfo, out var desktopResource).Success)
{
// Copy desktopResource → stagingTexture rồi Map/Unmap để đọc data
duplicatedOutput.ReleaseFrame();
}
– Ưu: API hiện đại, dễ dùng hơn DXGI, hỗ trợ capture một cửa sổ cụ thể hoặc toàn màn;
– Nhược: chỉ Win10 1903+; phải reference Microsoft.WinUI, Windows SDK; đôi khi phức tạp khi chạy WinForms/WPF.
Ví dụ ngắn
csharp
using Windows.Graphics.Capture;
using Windows.Graphics.DirectX.Direct3D11;
using Win32Interop = Windows.Graphics.Capture.Interop;
var picker = new GraphicsCapturePicker();
var item = await picker.PickSingleItemAsync();
if (item != null)
{
var framePool = Direct3D11CaptureFramePool.Create(
device,
DirectXPixelFormat.B8G8R8A8UIntNormalized,
2,
item.Size);
var session = framePool.CreateCaptureSession(item);
session.StartCapture();
// Đăng ký event FrameArrived để xử lý ảnh
}
Kết luận
• Nếu bạn chỉ cần chụp tĩnh (file ảnh, screenshot) và muốn tương thích max: Dùng GDI+ (BitBlt).
• Nếu bạn cần ghi video, hoặc FPS cao, đa màn với độ trễ thấp: Dùng Desktop Duplication API (DXGI).
• Muốn code gọn hơn, hướng UWP/WinRT: Dùng Windows.Graphics.Capture (Win10+).
Chúc bạn thành công! Nếu cần thêm demo chi tiết hay gói nuget cụ thể, cứ hỏi tiếp nhé 😊