private void DrawPreview() { ImageCaptureInfo copy = imageCaptureInfo; copy.captureSizeX = previewPictureBox.Width; copy.captureSizeY = previewPictureBox.Height; //Show something in the preview previewImage = CaptureImageFullPreview(ref copy); float crop_size_x = copy.actual_crop_size_x; float crop_size_y = copy.actual_crop_size_y; //Draw selection rectangle DrawCaptureRectangleBitmap(); //Compute image crop coordinates according to selection rectangle //Get raw image size from imageCaptureInfo.actual_crop_size to compute scaling between raw and rectangle coordinates //Console.WriteLine("SIZE X: {0}, SIZE Y: {1}", imageCaptureInfo.actual_crop_size_x, imageCaptureInfo.actual_crop_size_y); imageCaptureInfo.crop_coordinate_left = selectionRectanglePreviewBox.Left * (crop_size_x / previewPictureBox.Width); imageCaptureInfo.crop_coordinate_right = selectionRectanglePreviewBox.Right * (crop_size_x / previewPictureBox.Width); imageCaptureInfo.crop_coordinate_top = selectionRectanglePreviewBox.Top * (crop_size_y / previewPictureBox.Height); imageCaptureInfo.crop_coordinate_bottom = selectionRectanglePreviewBox.Bottom * (crop_size_y / previewPictureBox.Height); copy.crop_coordinate_left = selectionRectanglePreviewBox.Left * (crop_size_x / previewPictureBox.Width); copy.crop_coordinate_right = selectionRectanglePreviewBox.Right * (crop_size_x / previewPictureBox.Width); copy.crop_coordinate_top = selectionRectanglePreviewBox.Top * (crop_size_y / previewPictureBox.Height); copy.crop_coordinate_bottom = selectionRectanglePreviewBox.Bottom * (crop_size_y / previewPictureBox.Height); croppedPreviewPictureBox.Image = CaptureImageFullPreview(ref copy, useCrop: true); copy.captureSizeX = captureSize.Width; copy.captureSizeY = captureSize.Height; //Show matching bins for preview var capture = CaptureImage(); var features = FeatureDetector.featuresFromBitmap(capture); int tempMatchingBins = 0; var isLoading = FeatureDetector.compareFeatureVector(features.ToArray(), out tempMatchingBins, false); matchDisplayLabel.Text = tempMatchingBins.ToString(); }
public static Bitmap CaptureFromDisplay(ref ImageCaptureInfo info) { 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); } 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 if (((float)device_width / (float)device_height) > (info.captureAspectRatio)) { 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; } else { var image_region = (float)device_width / (info.captureAspectRatio); //Aspect ratio is larger than original var black_bar_height_total = (float)device_height - image_region; //Compute space occupied by black border relative to total width var adjust_factor = ((float)(device_height - black_bar_height_total) / (float)device_height); info.actual_crop_size_y *= adjust_factor; info.actual_offset_y *= 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) { 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); }
//amount of variance allowed for correct comparison public CrashNSaneLoadDetector() { InitializeComponent(); captureTimer = new System.Timers.Timer(); captureTimer.Elapsed += recordOrDetect; oldGameTime = DateTime.Now; oldRealTime = DateTime.Now; captureTimer.Interval = timerIntervalMilliseconds; gameTimer = new System.Timers.Timer(); realTimer = new System.Timers.Timer(); gameTimer.Interval = timerIntervalMilliseconds; realTimer.Interval = timerIntervalMilliseconds; gameTimer.Elapsed += timerValueUpdate; realTimer.Elapsed += timerValueUpdate; snapshotMilliseconds = milliSecondsBetweenSnapshots; listOfFeatureVectors = new List <List <int> >(); //histogramMatches = new List<bool>(); msElapsed = new List <long>(); lastFeatures = new List <int>(); segmentSnapshots = new List <Bitmap>(); segmentMatchingBins = new List <int>(); segmentFrameCounts = new List <int>(); segmentFeatureVectors = new List <List <int> >(); HistogramOfMatchingBins = new int[577]; selectionTopLeft = new Point(0, 0); selectionBottomRight = new Point(previewPictureBox.Width, previewPictureBox.Height); selectionRectanglePreviewBox = new Rectangle(selectionTopLeft.X, selectionTopLeft.Y, selectionBottomRight.X - selectionTopLeft.X, selectionBottomRight.Y - selectionTopLeft.Y); requiredMatchesUpDown.Value = FeatureDetector.numberOfBinsCorrect; StartKeyHandler = new KeyHandler(Keys.NumPad1, this); StartKeyHandler.Register(); ResetKeyHandler = new KeyHandler(Keys.NumPad3, this); ResetKeyHandler.Register(); Process[] processListtmp = Process.GetProcesses(); List <Process> processes_with_name = new List <Process>(); processListBox.Items.Clear(); numScreens = 0; foreach (var screen in Screen.AllScreens) { // For each screen, add the screen properties to a list box. processListBox.Items.Add("Screen: " + screen.DeviceName + ", " + screen.Bounds.ToString()); numScreens++; } foreach (Process process in processListtmp) { if (!String.IsNullOrEmpty(process.MainWindowTitle)) { Console.WriteLine("Process: {0} ID: {1} Window title: {2} HWND PTR {3}", process.ProcessName, process.Id, process.MainWindowTitle, process.MainWindowHandle); processListBox.Items.Add(process.ProcessName + ": " + process.MainWindowTitle); processes_with_name.Add(process); } } processList = processes_with_name.ToArray(); imageCaptureInfo = new ImageCaptureInfo(); imageCaptureInfo.featureVectorResolutionX = featureVectorResolutionX; imageCaptureInfo.featureVectorResolutionY = featureVectorResolutionY; imageCaptureInfo.captureSizeX = captureSize.Width; imageCaptureInfo.captureSizeY = captureSize.Height; imageCaptureInfo.cropOffsetX = cropOffsetX; imageCaptureInfo.cropOffsetY = cropOffsetY; imageCaptureInfo.captureAspectRatio = captureAspectRatioX / captureAspectRatioY; processListBox.SelectedIndex = 0; }