public static Rectangle GetWindowRect(IntPtr hwnd) { Interop.RECT tmp_rect = new Interop.RECT(); Interop.GetWindowRect(hwnd, ref tmp_rect); Rectangle rect = new Rectangle(tmp_rect.left, tmp_rect.top, tmp_rect.right - tmp_rect.left, tmp_rect.bottom - tmp_rect.top); return rect; }
public static Bitmap CaptureScreenNew(IntPtr hwnd) { Image img = null; try { // get te hDC of the target window IntPtr hdcSrc = Interop.GetWindowDC(hwnd); // get the size Interop.RECT windowRect = new Interop.RECT(); Interop.GetWindowRect(hwnd, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = Interop.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = Interop.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = Interop.SelectObject(hdcDest, hBitmap); // bitblt over Interop.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Interop.SRCCOPY); // restore selection Interop.SelectObject(hdcDest, hOld); // clean up Interop.DeleteDC(hdcDest); Interop.ReleaseDC(hwnd, hdcSrc); // get a .NET image object for it img = Image.FromHbitmap(hBitmap); // free up the Bitmap object Interop.DeleteObject(hBitmap); } catch (Exception e) { Console.WriteLine("LOL"); } return (Bitmap)img; }