示例#1
0
        /// <summary>
        /// Event handler for buttonFind click event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonFind_Click(object sender, EventArgs e)
        {
            _foundBitmapLocations.Clear();

            var bitmapsToFind = _fileNames.Select(fileName => new Bitmap(fileName)).ToList();

            Cursor = Cursors.WaitCursor;

            using (var screenBitmap = BitmapOperations.GetFullScreenBitmap())
            {
                foreach (var smallBitmap in bitmapsToFind)
                {
                    if (smallBitmap.Height > screenBitmap.Height || smallBitmap.Width > screenBitmap.Width)
                    {
                        MessageBox.Show("One of the bitmaps is larger than the screen - it's not possible to find it.", "Invalid input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                    var foundLocations = BitmapOperations.SearchBitmap(smallBitmap, screenBitmap, 0).ToList();
                    _foundBitmapLocations.AddRange(foundLocations);

                    foreach (var foundLocation in foundLocations)
                    {
                        _foundTexts.Add(TextOperations.GetTextNearLocation(screenBitmap, foundLocation));
                    }

                    smallBitmap.Dispose();
                }
            }

            Cursor = Cursors.Default;

            if (_foundBitmapLocations.Count > 0)
            {
                var message =
                    new StringBuilder((bitmapsToFind.Count > 1 ? "The bitmaps were" : "The bitmap was") +
                                      " found at the following location(s):\n");
                for (var i = 0; i < _foundBitmapLocations.Count; i++)
                {
                    message.Append(String.Format("({0},{1}): The text on the right-hand side of the bitmap was '{2}'\n", _foundBitmapLocations[i].X, _foundBitmapLocations[i].Y, _foundTexts[i].Trim()));
                }
                MessageBox.Show(message.ToString());
            }
            else
            {
                var message =
                    new StringBuilder((bitmapsToFind.Count > 1 ? "The bitmaps were" : "The bitmap was") +
                                      " not found on the screen.");
                MessageBox.Show(message.ToString());
            }
        }
示例#2
0
        /// <summary>
        /// Parses and returns any text on the right-hand side of the location on the bitmap using Tesseract.
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="location"></param>
        /// <returns>The text that was found.</returns>
        public static string GetTextNearLocation(Bitmap bitmap, Point location)
        {
            //Just some sample values for debugging and testing...
            var croppedArea = new Rectangle(new Point(location.X + 40, location.Y), new Size(150, 17));

            using (var croppedBitmap = new Bitmap(croppedArea.Width, croppedArea.Height))
            {
                using (var graphics = Graphics.FromImage(croppedBitmap))
                {
                    graphics.DrawImage(bitmap, -croppedArea.X, -croppedArea.Y);

                    using (var scaledBitmap = BitmapOperations.ResizeBitmap(croppedBitmap, croppedBitmap.Width * 3, croppedBitmap.Height * 3))
                    {
                        using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
                        {
                            using (var page = engine.Process(scaledBitmap, PageSegMode.SingleLine))
                            {
                                return(page.GetText());
                            }
                        }
                    }
                }
            }
        }