private string TestOCR() { try { if (this.inPtr != IntPtr.Zero) { obj = new TextCaptureX(); return(obj.GetTextFromRect(this.inPtr.ToInt32(), Location.X + 20, Location.Y + 20, this.Width - 20, this.Height - 20)); } return(""); } catch (Exception ex) { return(""); } }
private void PerformRegionCapture(int hwnd, int x, int y, int width, int height) { if (m_bRegOrWnd == false) { // if a window was selected MessageBox.Show("You selected a window!"); return; } CaptureMethod currentMethod = GetCaptureMethod(); string strText = ""; if (currentMethod == CaptureMethod.NATIVE) { //stdole.IFontDisp font; try { // preserve layout checkbox if (checkLayout.Checked == true) { m_tCapture.FormattedText = true; } strText = m_tCapture.GetTextFromRect(hwnd, x, y, width, height); //txtFont.Text = font.Name; //txtFontSize.Text = font.Size.ToString(); } catch (Exception ex) { System.Runtime.InteropServices.COMException COMEx = (System.Runtime.InteropServices.COMException)ex; if (COMEx.ErrorCode == -2147220989 && m_bCanTryAgain) { // this is the timeout exception m_bCanTryAgain = false; MessageBox.Show("Trying again!"); System.Threading.Thread.Sleep(4000); // sleep for 4 seconds PerformRegionCapture(hwnd, x, y, width, height); // and try again return; } else { strText = "Capture Error!"; MessageBox.Show(ex.Message); } } } else if (currentMethod == CaptureMethod.FULL_TEXT) { try { strText = m_tFullTextCapt.GetFullTextFromRectangle(hwnd, x, y, width, height); txtFont.Text = ""; } catch (Exception ex) { System.Runtime.InteropServices.COMException COMEx = (System.Runtime.InteropServices.COMException)ex; if (COMEx.ErrorCode == -2147220989 && m_bCanTryAgain) { // this is the timeout exception m_bCanTryAgain = false; MessageBox.Show("Trying again!"); System.Threading.Thread.Sleep(5000); // sleep for 4 seconds PerformRegionCapture(hwnd, x, y, width, height); // and try again return; } else { strText = "Capture Error!"; MessageBox.Show(ex.Message); } } } else if (currentMethod == CaptureMethod.OCR) { try { string strLang = (string)comboLang.Items[comboLang.SelectedIndex]; strText = m_tOCRCapture.GetTextFromRect(hwnd, x, y, width, height, strLang, false); txtFont.Text = ""; } catch (Exception ex) { System.Runtime.InteropServices.COMException COMEx = (System.Runtime.InteropServices.COMException)ex; if (COMEx.ErrorCode == -2147220989 && m_bCanTryAgain) { // this is the timeout exception m_bCanTryAgain = false; MessageBox.Show("Trying again!"); System.Threading.Thread.Sleep(5000); // sleep for 4 seconds PerformRegionCapture(hwnd, x, y, width, height); // and try again return; } else { strText = "Capture Error!"; MessageBox.Show(ex.Message); } } } txtResult.Text = strText; }