public static Bitmap PrintWindow(IntPtr hwnd, ref ImageCaptureInfo info, bool full = false, bool useCrop = false, float scalingValueFloat = 1.0f) { try { 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 resoution 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); return(ResizeImage(ret, info.captureSizeX, info.captureSizeY)); } catch (System.Runtime.InteropServices.ExternalException ex) { return(new Bitmap(10, 10)); } }
private Bitmap CaptureImageFullPreview(ref ImageCaptureInfo imageCaptureInfo, bool useCrop = false) { Bitmap b = new Bitmap(1, 1); //Full screen capture if (processCaptureIndex < 0) { Screen selected_screen = Screen.AllScreens[-processCaptureIndex - 1]; Rectangle screenRect = selected_screen.Bounds; screenRect.Width = (int)(screenRect.Width * scalingValueFloat); screenRect.Height = (int)(screenRect.Height * scalingValueFloat); Point screenCenter = new Point((int)(screenRect.Width / 2.0f), (int)(screenRect.Height / 2.0f)); if (useCrop) { //Change size according to selected crop screenRect.Width = (int)(imageCaptureInfo.crop_coordinate_right - imageCaptureInfo.crop_coordinate_left); screenRect.Height = (int)(imageCaptureInfo.crop_coordinate_bottom - imageCaptureInfo.crop_coordinate_top); } //Compute crop coordinates and width/ height based on resoution ImageCapture.SizeAdjustedCropAndOffset(screenRect.Width, screenRect.Height, ref imageCaptureInfo); imageCaptureInfo.actual_crop_size_x = 2 * imageCaptureInfo.center_of_frame_x; imageCaptureInfo.actual_crop_size_y = 2 * imageCaptureInfo.center_of_frame_y; if (useCrop) { //Adjust for crop offset imageCaptureInfo.center_of_frame_x += imageCaptureInfo.crop_coordinate_left; imageCaptureInfo.center_of_frame_y += imageCaptureInfo.crop_coordinate_top; } //Adjust for selected screen offset imageCaptureInfo.center_of_frame_x += selected_screen.Bounds.X; imageCaptureInfo.center_of_frame_y += selected_screen.Bounds.Y; imageCaptureInfo.actual_offset_x = 0; imageCaptureInfo.actual_offset_y = 0; b = ImageCapture.CaptureFromDisplay(ref imageCaptureInfo); imageCaptureInfo.actual_offset_x = cropOffsetX; imageCaptureInfo.actual_offset_y = cropOffsetY; } else { IntPtr handle = new IntPtr(0); if (processCaptureIndex >= processList.Length) { return(b); } if (processCaptureIndex != -1) { handle = processList[processCaptureIndex].MainWindowHandle; } //Capture from specific process processList[processCaptureIndex].Refresh(); if ((int)handle == 0) { return(b); } b = ImageCapture.PrintWindow(handle, ref imageCaptureInfo, full: true, useCrop: useCrop, scalingValueFloat: scalingValueFloat); } return(b); }