C#

C# WinAPI - Window Capture

hwanjung 2022. 3. 24. 13:17
using System.Runtime.InteropServices;
using System.Drawing;

[DllImport("user32.dll")]
private static extern Boolean PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);

private Bitmap WinCapture(IntPtr handle)
{
	using (Graphics newGraphics = Graphics.FromHwnd(handle))
	{
		Rectangle rect = Rectangle.Round(newGraphics.VisibleClipBounds);
		Bitmap bmp = new Bitmap(rect.Width, rect.Height);
		using (Graphics g = Graphics.FromImage(bmp))
		{
			IntPtr hdc = g.GetHdc();
			PrintWindow(handle, hdc, 0x2);
			g.ReleaseHdc(hdc);
		}
		return bmp;
	}
}