/// <summary> /// 根据窗口HWND截取窗口 /// </summary> /// <param name="handle"></param> /// <returns></returns> public static Bitmap GetWindowCapture(IntPtr handle) { // get te hDC of the target window IntPtr hdcSrc = GetWindowDC(handle); ShowWindow(hdcSrc, 1); // get the size SC_RECT windowRect = new SC_RECT(); GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = SelectObject(hdcDest, hBitmap); // bitblt over BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY | CAPTUREBLT); // restore selection SelectObject(hdcDest, hOld); // clean up DeleteDC(hdcDest); ReleaseDC(handle, hdcSrc); // get a .NET image object for it Bitmap img = Image.FromHbitmap(hBitmap); // free up the Bitmap object DeleteObject(hBitmap); return(img); }
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref SC_RECT rect);