public static Bitmap CaptureFromDisplay(ref ImageCaptureInfo info, bool useResize = false) { Bitmap b = new Bitmap((int)info.actual_crop_size_x, (int)info.actual_crop_size_y); //Full screen capture using (Graphics g = Graphics.FromImage(b)) { g.CopyFromScreen((int)(info.center_of_frame_x - info.actual_crop_size_x / 2 + info.actual_offset_x), (int)(info.center_of_frame_y - info.actual_crop_size_y / 2 + info.actual_offset_y), 0, 0, new Size((int)info.actual_crop_size_x, (int)info.actual_crop_size_y), CopyPixelOperation.SourceCopy); } if (useResize) { if (info.resizeState == ResizeState.ENG) { b = ResizeImage(b, info.resizeSizeXEng, info.resizeSizeYEng); } else { b = ResizeImage(b, info.resizeSizeXJpn, info.resizeSizeYJpn); } } else { b = ResizeImage(b, info.captureSizeX, info.captureSizeY); } return(b); }
//Should probably refactor this into some kind of struct, whatever... public static void SizeAdjustedCropAndOffset(int device_width, int device_height, ref ImageCaptureInfo info) { var resolution_factor_x = (device_width / info.featureVectorResolutionX); var resolution_factor_y = (device_height / info.featureVectorResolutionY); info.actual_crop_size_x = info.captureSizeX * resolution_factor_x; info.actual_crop_size_y = info.captureSizeY * resolution_factor_y; //Scale offset depending on resolution info.actual_offset_x = info.cropOffsetX * resolution_factor_x; info.actual_offset_y = info.cropOffsetY * resolution_factor_y; //Scale offset and sizes depending on actual vs. needed aspect ratio var image_region = (float)device_height / (1.0f / info.captureAspectRatio); //Aspect ratio is larger than original var black_bar_width_total = (float)device_width - image_region; //Compute space occupied by black border relative to total width var adjust_factor = ((float)(device_width - black_bar_width_total) / (float)device_width); info.actual_crop_size_x *= adjust_factor; info.actual_offset_x *= adjust_factor; info.center_of_frame_x = device_width / 2; info.center_of_frame_y = device_height / 2; }
public static Bitmap PrintWindow(IntPtr hwnd, ref ImageCaptureInfo info, bool full = false, bool useCrop = false, float scalingValueFloat = 1.0f, bool useResize = false) { 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); if (useResize) { if (info.resizeState == ResizeState.ENG) { ret = ResizeImage(ret, info.resizeSizeXEng, info.resizeSizeYEng); } else { ret = ResizeImage(ret, info.resizeSizeXJpn, info.resizeSizeYJpn); } } else { ret = ResizeImage(ret, info.captureSizeX, info.captureSizeY); } return(ret); } catch (System.Runtime.InteropServices.ExternalException ex) { return(new Bitmap(10, 10)); } }