public static extern bool GetMonitorInfoW(IntPtr monitor, [In, Out] MONITORINFOEX info);
/// <summary> /// Captures the screen containing mouse pointer. /// </summary> /// <param name="includeCursor"> /// <c>true</c> to capture the mouse pointer; otherwise, <c>false</c>. /// </param> /// <returns> /// The snapshot of the current screen. /// </returns> private static Bitmap GetCurrentScreenImage(bool includeCursor) { POINT screenId; MONITORINFOEX monitorInfo = new MONITORINFOEX(); if (NativeMethods.GetCursorPos(out screenId) && NativeMethods.GetMonitorInfoW(NativeMethods.MonitorFromPoint(screenId, NativeMethods.MONITOR_DEFAULTTONEAREST), monitorInfo)) { int width = monitorInfo.rcMonitor.Right - monitorInfo.rcMonitor.Left; int height = monitorInfo.rcMonitor.Bottom - monitorInfo.rcMonitor.Top; Bitmap outputBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); using (Graphics outputGraphics = Graphics.FromImage(outputBitmap)) { using (DeviceContext sourceDC = new DeviceContext(new string(monitorInfo.szDevice))) { IntPtr outputDC = outputGraphics.GetHdc(); try { NativeMethods.BitBlt(new HandleRef(outputGraphics, outputDC), width, height, new HandleRef(sourceDC, sourceDC.Handle), 0, 0); if (includeCursor) { CURSORINFO cursorInfo; cursorInfo.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); if (NativeMethods.GetCursorInfo(out cursorInfo) && (cursorInfo.flags == NativeMethods.CURSOR_SHOWING)) { using (IconData icon = new IconData(cursorInfo.hCursor)) { NativeMethods.DrawItem(new HandleRef(outputGraphics, outputDC), cursorInfo.ptScreenPos.X - monitorInfo.rcMonitor.Left - icon.HotSpotX, cursorInfo.ptScreenPos.Y - monitorInfo.rcMonitor.Top - icon.HotSpotY, new HandleRef(icon, cursorInfo.hCursor)); } } } return outputBitmap; } finally { outputGraphics.ReleaseHdc(); } } } } return null; }