public static Bitmap CaptureDWM() { if (thumb != IntPtr.Zero) { DwmUnregisterThumbnail(thumb); } int windowWidth; if (DebugForm.WindowRect.X > DebugForm.WindowRect.Width) { windowWidth = DebugForm.WindowRect.X - DebugForm.WindowRect.Width; } else { windowWidth = DebugForm.WindowRect.Width - DebugForm.WindowRect.X; } var windowHeight = DebugForm.WindowRect.Height - DebugForm.WindowRect.Y; var dwmCaptureForm = new Form { Text = "DWM Capture Window", Width = windowWidth + 15, Height = windowHeight + 32, Left = 0, Top = 0, TopMost = true }; dwmCaptureForm.Show(); int i = DwmRegisterThumbnail(dwmCaptureForm.Handle, DebugForm.WindowHandle, out thumb); if (i == 0) { DWMFunctions.UpdateThumb(); } DebugForm.GetWindowRect(dwmCaptureForm.Handle, out var dwmCapRectangle); var dwmCapture = new CaptureImage().CaptureFromScreen(dwmCapRectangle); DebugForm.DebugPictureBox.Invoke( new MethodInvoker(delegate { DebugForm.DebugPictureBox.Image = dwmCapture; })); DwmUnregisterThumbnail(thumb); dwmCaptureForm.Close(); return(dwmCapture); }