//Get screenshot on the center of game public static System.Drawing.Image CaptureWindow(string name, bool followMouse) { if (Process.GetProcessesByName(name).Count() == 0) { MessageBox.Show($"Looks like you closed {name}..."); Process.GetCurrentProcess().Kill(); } IntPtr handle = Process.GetProcessesByName(name)[0].MainWindowHandle; IntPtr hdcSrc = User32.GetWindowDC(handle); User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); screen_width = windowRect.right - windowRect.left; screen_height = windowRect.bottom - windowRect.top; IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, size.X, size.Y); IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); if (followMouse) { GDI32.BitBlt(hdcDest, 0, 0, size.X, size.Y, hdcSrc, coordinates.X - size.X / 2, coordinates.Y - size.Y / 2, GDI32.SRCCOPY); } else { GDI32.BitBlt(hdcDest, 0, 0, size.X, size.Y, hdcSrc, screen_width / 2 - size.X / 2, screen_height / 2 - size.Y / 2, GDI32.SRCCOPY); } GDI32.SelectObject(hdcDest, hOld); GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); System.Drawing.Image img = System.Drawing.Image.FromHbitmap(hBitmap); GDI32.DeleteObject(hBitmap); return(img); }
public void Dispose() { GDI32.SelectObject(hdcDest, hOld); GDI32.DeleteDC(hdcDest); User32.ReleaseDC(phnd, hdcSrc); GDI32.DeleteObject(hBitmap); }