/// <summary> /// Takes the snapshot of the foreground window /// </summary> /// <returns>Bitmap object</returns> public static Bitmap ForegroundSnapshot() { IntPtr hwnd = USER32.GetForegroundWindow(); if (hwnd == IntPtr.Zero) { return(null); } else { USER32.RECT rc; USER32.GetWindowRect(hwnd, out rc); Rectangle r = ApiConverter.ToRectangle(rc); Bitmap bitmap = new Bitmap(r.Width, r.Height, PixelFormat.Format24bppRgb); using (Graphics g = Graphics.FromImage(bitmap)) { IntPtr hdcBitmap = g.GetHdc(); USER32.PrintWindow(hwnd, hdcBitmap, 0); } return(bitmap); } }