Screenshot class to do some pre-processing before running the OCR algorithm
Inheritance: IDisposable
示例#1
0
        /// <summary>
        /// run Ocr-Preprocessing filters
        /// TODO: run pre-OCR optimizations on separate thread/process
        /// </summary>
        /// <param name="screenshot"></param>
        private static void RunOcrPreProcessing(Screenshot screenshot)
        {
            //return;
            try
            {
                //screenshot.Crop(); //TODO: re-enable when screenshotter works again
                //screenshot.Resize(); // resize to 300dpi //TODO: neccessary for screenshot?

                screenshot.ToGrayscale();
                //screenshot.SubtractMedianBlur(); //TODO: how to make it work?
                screenshot.ToBinary();
            }
            catch (Exception e)
            {
                Logger.WriteToLogFile(e);
            }
        }
        /// <summary>
        /// creates and returns the screenshot of the currently active window
        /// </summary>
        /// <returns></returns>
        private static Screenshot CaptureWindow()
        {
            try
            {
                var ptrHandle = GetForegroundWindow(); // currently active window
                var rect = new Rect();

                //var test = Screen.AllScreens; // => http://stackoverflow.com/questions/3827367/how-can-i-take-a-picture-from-screen (get rect for screen)

                // screen resolution stuff (device-independent units -> Pixels)
                double dpiX;
                double dpiY;
                using (var g = Graphics.FromHwnd(IntPtr.Zero)) // use using to dispose afterwards
                {
                    dpiX = (g.DpiX / 96.0); //TODO: directly here to 300 dpi?
                    dpiY = (g.DpiY / 96.0);
                }

                rect.Right = Convert.ToInt32(rect.Right / dpiX);
                rect.Left = Convert.ToInt32(rect.Left / dpiX);
                rect.Bottom = Convert.ToInt32(rect.Bottom / dpiY);
                rect.Top = Convert.ToInt32(rect.Top / dpiY);

                // get bounds, etc.
                GetWindowRect(ptrHandle, ref rect);
                var width = Math.Abs(rect.Right - rect.Left);
                var height = Math.Abs(rect.Bottom - rect.Top);
                var bounds = new Rectangle(rect.Left, rect.Top, width, height);
                var result = new Bitmap(bounds.Width, bounds.Height);

                // get screenshot
                using (var graphics = Graphics.FromImage(result)) // use using to dispose afterwards
                {
                    graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                    graphics.Dispose();
                }

                var sc = new Screenshot(result);
                return sc;
            }
            catch (Exception e)
            {
                Logger.WriteToLogFile(e);
            }

            return null;
        }