windowcapture
исходный код / UI/EditorForm.Ocr.cs

EditorForm.Ocr.cs

52 строк · 1,979 байт · модуль UI
 1using System;
 2using System.Drawing;
 3using System.Windows.Forms;
 4using WindowCapture.Helpers;
 5
 6namespace WindowCapture.UI
 7{
 8    public partial class EditorForm
 9    {
10        /// <summary>
11        /// "Grab text" (OCR): recognize text in the current image via the local Windows-OCR helper
12        /// and copy it to the clipboard. Bound to Ctrl+T. Runs synchronously (OCR of one image is
13        /// fast) — kept simple; could be backgrounded later if large captures cause a hitch.
14        /// </summary>
15        private void GrabText()
16        {
17            if (!OcrClient.IsAvailable)
18            {
19                ToastNotification.Show("OCR", "Модуль распознавания не найден (bin/Ocr/WC.Ocr.exe)", true);
20                return;
21            }
22
23            Bitmap composite = null;
24            bool disposeComposite = false;
25            try
26            {
27                if (canvas != null) { composite = canvas.GetFullCompositeImage(); disposeComposite = composite != null; }
28                if (composite == null) composite = captured;
29                if (composite == null) return;
30
31                string text = OcrClient.Recognize(composite, "ru");
32                if (!string.IsNullOrWhiteSpace(text))
33                {
34                    try { Clipboard.SetText(text.Trim()); } catch { }
35                    ToastNotification.Show("OCR", "Текст распознан и скопирован в буфер", false);
36                }
37                else
38                {
39                    ToastNotification.Show("OCR", "Текст на изображении не найден", true);
40                }
41            }
42            catch (Exception ex)
43            {
44                ToastNotification.Show("OCR", "Ошибка распознавания: " + ex.Message, true);
45            }
46            finally
47            {
48                if (disposeComposite && composite != null) composite.Dispose();
49            }
50        }
51    }
52}