private void DrawPreview()
        {
            try
            {
                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;

                lastFullCapture = previewImage;
                //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);

                Bitmap full_cropped_capture = CaptureImageFullPreview(ref copy, useCrop: true);
                croppedPreviewPictureBox.Image = full_cropped_capture;
                lastFullCroppedCapture         = full_cropped_capture;

                copy.captureSizeX = captureSize.Width;
                copy.captureSizeY = captureSize.Height;

                //Show matching bins for preview
                var        capture = CaptureImage();
                List <int> dummy;
                List <int> dummy2;
                int        black_level      = 0;
                var        features         = FeatureDetector.featuresFromBitmap(capture, out dummy, out black_level, out dummy2);
                int        tempMatchingBins = 0;
                var        isLoading        = FeatureDetector.compareFeatureVector(features.ToArray(), FeatureDetector.listOfFeatureVectorsEng, out tempMatchingBins, -1.0f, false);

                lastFeatures           = features;
                lastDiagnosticCapture  = capture;
                lastMatchingBins       = tempMatchingBins;
                matchDisplayLabel.Text = Math.Round((Convert.ToSingle(tempMatchingBins) / Convert.ToSingle(FeatureDetector.listOfFeatureVectorsEng.GetLength(1))), 4).ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.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);
        }
        private void initImageCaptureInfo()
        {
            imageCaptureInfo = new ImageCaptureInfo();

            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);

            imageCaptureInfo.featureVectorResolutionX = featureVectorResolutionX;
            imageCaptureInfo.featureVectorResolutionY = featureVectorResolutionY;
            imageCaptureInfo.captureSizeX             = captureSize.Width;
            imageCaptureInfo.captureSizeY             = captureSize.Height;
            imageCaptureInfo.cropOffsetX        = cropOffsetX;
            imageCaptureInfo.cropOffsetY        = cropOffsetY;
            imageCaptureInfo.captureAspectRatio = captureAspectRatioX / captureAspectRatioY;
        }
        //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)
            {
                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);
        }