public static Bitmap CaptureForegroundWindow() { //This function currently uses hardcoded values and is unused, but might be useful Rectangle bounds; var foregroundWindowsHandle = DLLImportStuff.GetForegroundWindow(); DLLImportStuff.GetClientRect(foregroundWindowsHandle, out bounds); //bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top); if (bounds.Width <= 0) { return(new Bitmap(1, 1)); } var result = new Bitmap(300, 100); using (var g = Graphics.FromImage(result)) { g.CopyFromScreen(new Point(bounds.Width / 2, bounds.Height / 2), new Point(bounds.X, bounds.Y), new Size(300, 100)); } return(result); }
public static Bitmap PrintWindow(IntPtr hwnd, ref ImageCaptureInfo info, bool full = false, bool useCrop = false, float scalingValueFloat = 1.0f) { try { var watch = System.Diagnostics.Stopwatch.StartNew(); Rectangle rc; DLLImportStuff.GetClientRect(hwnd, out rc); Bitmap ret = new Bitmap(1, 1); if (rc.Width < 0) { return(ret); } IntPtr hdcwnd = DLLImportStuff.GetDC(hwnd); IntPtr hdc = DLLImportStuff.CreateCompatibleDC(hdcwnd); rc.Width = (int)(rc.Width * scalingValueFloat); rc.Height = (int)(rc.Height * scalingValueFloat); if (useCrop) { //Change size according to selected crop rc.Width = (int)(info.crop_coordinate_right - info.crop_coordinate_left); rc.Height = (int)(info.crop_coordinate_bottom - info.crop_coordinate_top); } //Compute crop coordinates and width/ height based on resolution ImageCapture.SizeAdjustedCropAndOffset(rc.Width, rc.Height, ref info); float cropOffsetX = info.actual_offset_x; float cropOffsetY = info.actual_offset_y; if (full) { info.actual_offset_x = 0; info.actual_offset_y = 0; info.actual_crop_size_x = 2 * info.center_of_frame_x; info.actual_crop_size_y = 2 * info.center_of_frame_y; } if (useCrop) { //Adjust for crop offset info.center_of_frame_x += info.crop_coordinate_left; info.center_of_frame_y += info.crop_coordinate_top; } IntPtr hbmp = DLLImportStuff.CreateCompatibleBitmap(hdcwnd, (int)info.actual_crop_size_x, (int)info.actual_crop_size_y); DLLImportStuff.SelectObject(hdc, hbmp); DLLImportStuff.BitBlt(hdc, 0, 0, (int)info.actual_crop_size_x, (int)info.actual_crop_size_y, hdcwnd, (int)(info.center_of_frame_x + info.actual_offset_x - info.actual_crop_size_x / 2), (int)(info.center_of_frame_y + info.actual_offset_y - info.actual_crop_size_y / 2), DLLImportStuff.TernaryRasterOperations.SRCCOPY); info.actual_offset_x = cropOffsetX; info.actual_offset_y = cropOffsetY; ret = (Bitmap)Image.FromHbitmap(hbmp).Clone(); DLLImportStuff.DeleteObject(hbmp); DLLImportStuff.ReleaseDC(hwnd, hdcwnd); DLLImportStuff.DeleteDC(hdc); if (ret.Size.Width != info.featureSizeX && ret.Size.Height != info.featureSizeY) { ResizeNearestNeighbor filter = new ResizeNearestNeighbor(info.featureSizeX, info.featureSizeY); ret = filter.Apply(ret); } watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; return(ret); } catch (System.Runtime.InteropServices.ExternalException ex) { return(new Bitmap(10, 10)); } }