private Image CaptureWindow(IntPtr handle, int x, int y, int width, int height) { Image img = null; IntPtr srcDC = IntPtr.Zero; IntPtr memoryDC = IntPtr.Zero; IntPtr bitmap = IntPtr.Zero; try { // 해당 Handle의 크기 취득 if (width == 0 || height == 0) { var windowRect = new WindowNative.Rect(); var clientRect = new WindowNative.Rect(); WindowNative.GetWindowRect(handle, ref windowRect); WindowNative.GetClientRect(handle, ref clientRect); if (x == 0) { x = windowRect.Left; } if (y == 0) { y = windowRect.Top; } if (width == 0) { width = clientRect.Width; } if (height == 0) { height = windowRect.Height; } // Border 사이즈 제외 int diff = windowRect.Width - clientRect.Width; if (diff > 0) { int borderSize = diff / 2; x += borderSize; y += borderSize; height -= borderSize * 2; } } // 해상도 스케일에 의한 크기 변경 float scale = CalcScale(); if (scale > 1) { width = (int)(width * scale); height = (int)(height * scale); } handle = WindowNative.GetDesktopWindow(); srcDC = WindowNative.GetWindowDC(handle); memoryDC = WindowNative.CreateCompatibleDC(srcDC); bitmap = WindowNative.CreateCompatibleBitmap(srcDC, width, height); IntPtr oldBitmap = WindowNative.SelectObject(memoryDC, bitmap); WindowNative.BitBlt(memoryDC, 0, 0, width, height, srcDC, x, y, WindowNative.SRCCOPY | WindowNative.CAPTUREBLT); WindowNative.SelectObject(memoryDC, oldBitmap); img = Image.FromHbitmap(bitmap); } catch (Exception e) { Logger.Error(e); } finally { WindowNative.DeleteObject(bitmap); WindowNative.DeleteDC(memoryDC); WindowNative.ReleaseDC(handle, srcDC); } return(img); }