LEAD Technologies, Inc

OCR Tutorial - Working with Recognition Results

Take the following steps to create and run a program that shows how to interactively draw zones on an OCR page and how to recognize these zones. Remember, the purpose of the tutorials is to provide you with a quick and easy way to generate an OCR program:

  1. Start Visual Studio 2005 or 2008

  2. Choose File->New->Project from the menu

  3. In the New Project dialog box, choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application" in Visual Studio 2005 or "Windows Forms Application" in Visual Studio 2008 from the Templates List

  4. Type the project name as "OcrTutorial2" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.

  5. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to LEADTOOLS For .NET "<LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32" folder and select the following DLLs:

    Leadtools.dll Leadtools.Drawing.dll Leadtools.Codecs.dll Leadtools.WinForms.dll Leadtools.Forms.dll Leadtools.Forms.DocumentWriters.dll Leadtools.Forms.Ocr.dll Leadtools.Forms.Ocr.Professional.dll Leadtools.Codecs.Bmp.dll Leadtools.Codecs.Cmp.dll Leadtools.Codecs.Tif.dll Leadtools.Codecs.Fax.dll

    Note: The Leadtools.Codecs.*.dll references added are for the BMP, JPG, CMP, TIF and FAX image file formats. Add any additional file format codec DLL if required in your application.

  6. Drag and drop a text box to your form. Leave the name as default (textBox1) and change the following properties:

    Property Value Dock Top Multiline True ScrollBars Both Size 460, 100

  7. If you have not added the RasterImageViewer control to the Visual Studio toolbox, then select Tools->Choose Toolbox Items... from the menu, in the dialog box that show up, select the .NET Framework Components tab, click the Browse button and browse to LEADTOOLS For .NET "<LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32" folder and select Leadtools.WinForms.dll. Hit OK to add the Leadtools.WinForms controls to your Visual Studio Toolbox (Including the RasterImageViewer control).

  8. Drag and drop an instance of the RasterImageViewer control to your form. Leave the name as default "rasterImageViewer1" and change the following properties:

    Property Value Dock Fill InteractiveMode None UseDpi True

  9. Using the Visual Studio designer, right click on rasterImageViewer1 and select Properties. Select the Events tab and add handlers to the following events:

    Event Handler MouseDown rasterImageViewer1_MouseDown MouseMove rasterImageViewer1_MouseMove MouseUp rasterImageViewer1_MouseUp KeyDown rasterImageViewer1_KeyDown

  10. Switch to Form1 code view (Right-click Form1 in the solution explorer then select View Code) and add the following lines at the beginning of the file after any Importsor usingsection if there are any:

    [Visual Basic]

          
    
                 Imports Leadtools
                 Imports Leadtools.Codecs
                 Imports Leadtools.Drawing
                 Imports Leadtools.WinForms
                 Imports Leadtools.Forms
                 Imports Leadtools.Forms.DocumentWriters
                 Imports Leadtools.Forms.Ocr
                      
                 
    
    
        
    

    [C#]

          
    
                 using Leadtools;
                 using Leadtools.Codecs;
                 using Leadtools.Drawing;
                 using Leadtools.WinForms;
                 using Leadtools.Forms;
                 using Leadtools.Forms.DocumentWriters;
                 using Leadtools.Forms.Ocr;
                      
                 
    
    
        
    
  11. Add the following private variable to the Form1 class:

    [Visual Basic]

          
    
                 ' The OCR engine instance
                 Private _ocrEngine As IOcrEngine
                  ' The OCR document
                 Private _ocrDocument As IOcrDocument
                 ' Are we currently rubber banding
                 Private _isRubberBanding As Boolean
                 ' The current rubber band rectangle
                 Private _rubberBandingRectangle As Rectangle
                 ' Flag to indicates if the rubber band rectangle is drawn
                 Private _isRubberBandingRectangleDrawn As Boolean
                 ' Did we clip the cursor?
                 Private _saveClipRectangle As Rectangle
                      
                 
    
    
        
    

    [C#]

          
    
                 // The OCR engine instance
                 private IOcrEngine _ocrEngine;
                 // The OCR document
                 private IOcrDocument _ocrDocument;
                 // Are we currently rubber banding
                 private bool _isRubberBanding;
                 // The current rubber band rectangle
                 private Rectangle _rubberBandingRectangle;
                 // Flag to indicates if the rubber band rectangle is drawn
                 private bool _isRubberBandingRectangleDrawn;
                 // Did we clip the cursor?
                 private Rectangle _saveClipRectangle;
                      
                 
    
    
        
    
  12. Add the following code to the Form1 constructor (in Visual Basic, you can copy/paste the whole Sub New code from here):

    [Visual Basic]

          
    
                 Sub New()
                    ' This call is required by the Windows Form Designer.
                    InitializeComponent()
                    ' Add any initialization after the InitializeComponent() call.
                    ' Unlock the OCR support
                    RasterSupport.SetLicense(RasterSupportType.OcrProfessional, "Replace with your own key here")
                    ' Unlock Document support (For ScaleToGray)
                    RasterSupport.SetLicense(RasterSupportType.Document, "Replace with your own key here")
                    ' Initialize the OCR engine
                    _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Professional, False)
                    ' Startup the engine
                    _ocrEngine.Startup(Nothing, Nothing, Nothing, Nothing)
                    ' Create the OCR document
                    _ocrDocument = _ocrEngine.DocumentManager.CreateDocument()
                    ' Load an image into the OCR document
                    Dim fileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif"
                    Dim page As IOcrPage = _ocrDocument.Pages.AddPage(fileName, Nothing)
                    ' Enable ScaleToGray painting on the viewer
                    ' This will greatly enhance how the image looks
                    Dim props As RasterPaintProperties = RasterImageViewer1.PaintProperties
                    props.PaintDisplayMode = props.PaintDisplayMode Or RasterPaintDisplayModeFlags.ScaleToGray
                    RasterImageViewer1.PaintProperties = props
                    ' We cannot handle the LostFocus event from the designer, so we will do it here
                    AddHandler RasterImageViewer1.LostFocus, AddressOf rasterImageViewer1_LostFocus
                    ' Show this image in the viewer
                    RasterImageViewer1.Image = page.GetRasterImage()
                    Text = "Use +/-/Enter on the keyboard to zoom in/out on the image. Use the mouse to draw a zone and recognize it"
                 End Sub
                      
                 
    
    
        
    
    [C#]
          
    
                 public Form1()
                 {
                    InitializeComponent();
                    // Unlock the OCR support
                    RasterSupport.SetLicense(RasterSupportType.OcrProfessional, "Replace with your own key here");
                    // Unlock Document support (For ScaleToGray)
                    RasterSupport.SetLicense(RasterSupportType.Document, "Replace with your own key here");
                    // Initialize the OCR engine
                    _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Professional, false);
                    // Startup the engine
                    _ocrEngine.Startup(null, null, null, null);
                    // Create the OCR document
                    _ocrDocument = _ocrEngine.DocumentManager.CreateDocument();
                    // Load an image into the OCR document
                    string fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif";
                    IOcrPage page = _ocrDocument.Pages.AddPage(fileName, null);
                    // Enable ScaleToGray painting on the viewer
                    // This will greatly enhance how the image looks
                    RasterPaintProperties props = rasterImageViewer1.PaintProperties;
                    props.PaintDisplayMode |= RasterPaintDisplayModeFlags.ScaleToGray;
                    rasterImageViewer1.PaintProperties = props;
                    // We cannot handle the LostFocus event from the designer, so we will do it here
                    rasterImageViewer1.LostFocus += new EventHandler(rasterImageViewer1_LostFocus);
                    // Show this image in the viewer
                    rasterImageViewer1.Image = page.GetRasterImage();
                    Text = "Use +/-/Enter on the keyboard to zoom in/out on the image. Use the mouse to draw a zone and recognize it";
                 }
                      
                 
    
    
        
    
  13. Override the Form1 closed event to add the code necessary to shutdown the OCR engine when the application terminates:

    [Visual Basic]

          
    
                 Protected Overrides Sub OnFormClosed(ByVal e As FormClosedEventArgs)
                    ' Destroy the document
                    _ocrDocument.Dispose()
                    ' Shutdown and dispose the engine
                    _ocrEngine.Dispose()
                    MyBase.OnFormClosed(e)
                 End Sub
                      
                 
    
    
        
    

    [C#]

          
    
                 protected override void OnFormClosed(FormClosedEventArgs e)
                 {
                    // Destroy the document
                    _ocrDocument.Dispose();
                    // Shutdown and dispose the engine
                    _ocrEngine.Dispose();
                    base.OnFormClosed(e);
                 }
                      
                 
    
    
        
    
  14. Add the following code to rasterImageViewer1MouseDownevent handler:

    [Visual Basic]

          
    
                 Private Sub RasterImageViewer1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RasterImageViewer1.MouseDown
                    RasterImageViewer1.Focus()
                    ' Cancel rubber banding if it is on
                    If (_isRubberBanding) Then
                       EndRubberBanding()
                    Else
                       If (e.Button = MouseButtons.Left) Then
                          ' See if the click is on the image
                          Dim rect As Rectangle = RasterImageViewer1.PhysicalViewRectangle
                          If (rect.Contains(e.X, e.Y)) Then
                             ' Start the rubber banding
                             BeginRubberBanding(Rectangle.FromLTRB(e.X, e.Y, e.X, e.Y))
                          End If
                       End If
                    End If
                 End Sub
                      
                 
    
    
        
    

    [C#]

          
    
                 private void rasterImageViewer1_MouseDown(object sender, MouseEventArgs e)
                 {
                    rasterImageViewer1.Focus();
                    // Cancel rubber banding if it is on
                    if(_isRubberBanding)
                       EndRubberBanding();
                    else
                    {
                       if(e.Button == MouseButtons.Left)
                       {
                          // See if the click is on the image
                          Rectangle rect = rasterImageViewer1.PhysicalViewRectangle;
                          if(rect.Contains(e.X, e.Y))
                          {
                             // Start the rubber banding
                             BeginRubberBanding(Rectangle.FromLTRB(e.X, e.Y, e.X, e.Y));
                          }
                       }
                    }
                 }
                      
                 
    
    
        
    
  15. Add the following code to the rasterImageViewer1MouseMoveevent handler:

    [Visual Basic]

          
    
                 Private Sub RasterImageViewer1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RasterImageViewer1.MouseMove
                    ' If we are rubber banding, erase the old rectangle, update the position and draw the new rectangle
                    If (_isRubberBanding) Then
                       DrawRubberBandRectangle()
                       _rubberBandingRectangle = Rectangle.FromLTRB( _
                          _rubberBandingRectangle.Left, _
                          _rubberBandingRectangle.Top, _
                          e.X, _
                          e.Y)
                       DrawRubberBandRectangle()
                    End If
                 End Sub
                      
                 
    
    
        
    

    [C#]

          
    
                 private void rasterImageViewer1_MouseMove(object sender, MouseEventArgs e)
                 {
                    // If we are rubber banding, erase the old rectangle, update the position and draw the new rectangle
                    if(_isRubberBanding)
                    {
                       DrawRubberBandRectangle();
                       _rubberBandingRectangle = Rectangle.FromLTRB(
                          _rubberBandingRectangle.Left,
                          _rubberBandingRectangle.Top,
                          e.X,
                          e.Y);
                       DrawRubberBandRectangle();
                    }
                 }
                      
                 
    
    
        
    
  16. Add the following code to the rasterImageViewer1MouseUpevent handler:

    [Visual Basic]

          
    
                 Private Sub RasterImageViewer1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RasterImageViewer1.MouseUp
                    ' If we are rubber banding, end it
                    If (_isRubberBanding) Then
                       EndRubberBanding()
                       Dim bounds As RectangleF = _rubberBandingRectangle
                       ' This rectangle is in physical coordinates, convert it to logical
                       Using matrix As System.Drawing.Drawing2D.Matrix = RasterImageViewer1.GetTransformWithDpi()
                          Dim t As New Transformer(matrix)
                          bounds = t.RectangleToLogical(bounds)
                       End Using
                       ' Add the zone and recognize it
                       AddAndRecognizeZone(bounds)
                    End If
                 End Sub
                      
                 
    
    
        
    

    [C#]

          
    
                 private void rasterImageViewer1_MouseUp(object sender, MouseEventArgs e)
                 {
                    // If we are rubber banding, end it
                    if(_isRubberBanding)
                    {
                       EndRubberBanding();
                       RectangleF bounds = _rubberBandingRectangle;
                       // This rectangle is in physical coordinates, convert it to logical
                       using(System.Drawing.Drawing2D.Matrix matrix = rasterImageViewer1.GetTransformWithDpi())
                       {
                          Transformer t = new Transformer(matrix);
                          bounds = t.RectangleToLogical(bounds);
                       }
                       // Add the zone and recognize it
                       AddAndRecognizeZone(bounds);
                    }
                 }
                      
                 
    
    
        
    
  17. Add the following code to the rasterImageViewer1LostFocusevent handler:

    [Visual Basic]

          
    
                 Private Sub RasterImageViewer1_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
                    ' If we are rubber banding, cancel it
                    If (_isRubberBanding) Then
                       EndRubberBanding()
                    End If
                 End Sub
                      
                 
    
    
        
    

    [C#]

          
    
                 private void rasterImageViewer1_LostFocus(object sender, EventArgs e)
                 {
                    // If we are rubber banding, cancel it
                    if(_isRubberBanding)
                       EndRubberBanding();
                 }
                      
                 
    
    
        
    
  18. Add the following code to the rasterImageViewer1KeyDownevent handler:

    [Visual Basic]

          
    
                 Private Sub RasterImageViewer1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RasterImageViewer1.KeyDown
                    ' Zoom in and out using the mouse keys
                    Const zoomRatio As Double = 1.2
                    Dim scaleFactor As Double = RasterImageViewer1.ScaleFactor
                    Select Case (e.KeyCode)
                       Case Keys.Add, Keys.Oemplus
                          ' Zoon in
                          scaleFactor *= zoomRatio
                          e.Handled = True
                       Case Keys.Subtract, Keys.OemMinus
                          ' Zone out
                          scaleFactor /= zoomRatio
                          e.Handled = True
                       Case Keys.Enter
                          scaleFactor = 1
                          e.Handled = True
                    End Select
                    Const minimumScaleFactor As Double = 0.05
                    Const maximumScaleFactor As Double = 11
                    scaleFactor = Math.Max(minimumScaleFactor, Math.Min(maximumScaleFactor, scaleFactor))
                    ' Check if scale factor has changed
                    If (scaleFactor <> RasterImageViewer1.ScaleFactor) Then
                       RasterImageViewer1.BeginUpdate()
                       ' Yes, zoom keeping the center of the image
                       ' Get what you see in physical coordinates
                       Dim rc As Rectangle = Rectangle.Intersect(RasterImageViewer1.PhysicalViewRectangle, RasterImageViewer1.ClientRectangle)
                       ' Get the center of what you see in physical coordinates
                       Dim center As New PointF(rc.Left + rc.Width \ 2, rc.Top + rc.Height \ 2)
                       Using matrix As System.Drawing.Drawing2D.Matrix = RasterImageViewer1.GetTransformWithDpi()
                          Dim t As New Transformer(matrix)
                          ' Get the center of what you see in logical coordinates
                          center = t.PointToLogical(center)
                       End Using
                       RasterImageViewer1.ScaleFactor = scaleFactor
                       ' Bring the original center into the view center
                       Using matrix As System.Drawing.Drawing2D.Matrix = RasterImageViewer1.GetTransformWithDpi()
                          Dim t As New Transformer(matrix)
                          ' Get the center of what you saw before the zoom in physical coordinates
                          center = t.PointToPhysical(center)
                       End Using
                       ' Bring the old center into the center of the view
                       RasterImageViewer1.CenterAtPoint(Point.Round(center))
                       RasterImageViewer1.EndUpdate()
                    End If
                 End Sub
                      
                 
    
    
        
    

    [C#]

          
    
                 private void rasterImageViewer1_KeyDown(object sender, KeyEventArgs e)
                 {
                    // Zoom in and out using the mouse keys
                    const double zoomRatio = 1.2;
                    double scaleFactor = rasterImageViewer1.ScaleFactor;
                    switch(e.KeyCode)
                    {
                       case Keys.Add:
                       case Keys.Oemplus:
                          // Zoon in
                          scaleFactor *= zoomRatio;
                          e.Handled = true;
                          break;
                       case Keys.Subtract:
                       case Keys.OemMinus:
                          // Zone out
                          scaleFactor /= zoomRatio;
                          e.Handled = true;
                          break;
                       case Keys.Enter:
                          scaleFactor = 1;
                          e.Handled = true;
                          break;
                    }
                    const double minimumScaleFactor = 0.05;
                    const double maximumScaleFactor = 11;
                    scaleFactor = Math.Max(minimumScaleFactor, Math.Min(maximumScaleFactor, scaleFactor));
                    // Check if scale factor has changed
                    if(scaleFactor != rasterImageViewer1.ScaleFactor)
                    {
                       rasterImageViewer1.BeginUpdate();
                       // Yes, zoom keeping the center of the image
                       // Get what you see in physical coordinates
                       Rectangle rc = Rectangle.Intersect(rasterImageViewer1.PhysicalViewRectangle, rasterImageViewer1.ClientRectangle);
                       // Get the center of what you see in physical coordinates
                       PointF center = new PointF(rc.Left + rc.Width / 2, rc.Top + rc.Height / 2);
                       using(System.Drawing.Drawing2D.Matrix matrix = rasterImageViewer1.GetTransformWithDpi())
                       {
                          Transformer t = new Transformer(matrix);
                          // Get the center of what you see in logical coordinates
                          center = t.PointToLogical(center);
                       }
                       rasterImageViewer1.ScaleFactor = scaleFactor;
                       // Bring the original center into the view center
                       using(System.Drawing.Drawing2D.Matrix matrix = rasterImageViewer1.GetTransformWithDpi())
                       {
                          Transformer t = new Transformer(matrix);
                          // Get the center of what you saw before the zoom in physical coordinates
                          center = t.PointToPhysical(center);
                       }
                       // Bring the old center into the center of the view
                       rasterImageViewer1.CenterAtPoint(Point.Round(center));
                       rasterImageViewer1.EndUpdate();
                    }
                 }
                      
                 
    
    
        
    
  19. Add the following private methods to Form1 to handle the rubber banding:

    [Visual Basic]

          
    
                 ' Begins the rubber banding operation
                 Private Sub BeginRubberBanding(ByVal rect As Rectangle)
                    _rubberBandingRectangle = rect
                    _isRubberBanding = True
                    RasterImageViewer1.Capture = True
                    ' Clip the cursor
                    ClipCursor(True)
                    DrawRubberBandRectangle()
                 End Sub
                 ' Ends the rubber banding operation
                 Private Sub EndRubberBanding()
                    ' Stop clipping the cursor
                    ClipCursor(False)
                    RasterImageViewer1.Capture = False
                    _isRubberBanding = False
                    If (_isRubberBandingRectangleDrawn) Then
                       DrawRubberBandRectangle()
                    End If
                 End Sub
                 ' Draws the rubberband rectangle on the viewer
                 Private Sub DrawRubberBandRectangle()
                    Dim rect As Rectangle = FixRectangle(_rubberBandingRectangle)
                    rect.Width = rect.Width + 1
                    rect.Height = rect.Height + 1
                    rect = RasterImageViewer1.RectangleToScreen(rect)
                    ControlPaint.DrawReversibleFrame(rect, Color.Transparent, FrameStyle.Thick)
                    _isRubberBandingRectangleDrawn = Not _isRubberBandingRectangleDrawn
                 End Sub
                 ' Clips the mouse cursor on the dimension of the image in the viewer
                 Private Sub ClipCursor(ByVal clip As Boolean)
                    If (clip) Then
                       Dim rect As Rectangle = Rectangle.Intersect(FixRectangle(RasterImageViewer1.PhysicalViewRectangle), RasterImageViewer1.ClientRectangle)
                       rect = RasterImageViewer1.RectangleToScreen(rect)
                       Dim prnt As Control = RasterImageViewer1.Parent
                       Do While (Not prnt Is Nothing)
                          rect = Rectangle.Intersect(rect, RasterImageViewer1.Parent.RectangleToScreen(RasterImageViewer1.Parent.ClientRectangle))
                          If (TypeOf prnt Is Form) Then
                             Dim f As Form = CType(prnt, Form)
                             If (f.IsMdiChild) Then
                                If (Not f.Owner Is Nothing) Then
                                   rect = Rectangle.Intersect(rect, f.Owner.RectangleToScreen(f.Owner.ClientRectangle))
                                ElseIf (Not f.Parent Is Nothing) Then
                                   rect = Rectangle.Intersect(rect, f.Parent.RectangleToScreen(f.Parent.ClientRectangle))
                                End If
                             End If
                          End If
                          prnt = prnt.Parent
                       Loop
                       rect.Height = rect.Height + 1
                       rect.Width = rect.Width + 1
                       _saveClipRectangle = Cursor.Clip
                       Cursor.Clip = rect
                    Else
                       Cursor.Clip = _saveClipRectangle
                       _saveClipRectangle = Rectangle.Empty
                    End If
                 End Sub
                 ' Fox the rectangle coordinates is inverted (its right or bottom value greater than its left or top)
                 Private Shared Function FixRectangle(ByVal rect As Rectangle) As Rectangle
                    If (rect.Left > rect.Right) Then
                       rect = Rectangle.FromLTRB(rect.Right, rect.Top, rect.Left, rect.Bottom)
                    End If
                    If (rect.Top > rect.Bottom) Then
                       rect = Rectangle.FromLTRB(rect.Left, rect.Bottom, rect.Right, rect.Top)
                    End If
                    Return rect
                 End Function
                      
                 
    
    
        
    

    [C#]

          
    
                 // Begins the rubber banding operation
                 private void BeginRubberBanding(Rectangle rect)
                 {
                    _rubberBandingRectangle = rect;
                    _isRubberBanding = true;
                    rasterImageViewer1.Capture = true;
                    // Clip the cursor
                    ClipCursor(true);
                    DrawRubberBandRectangle();
                 }
                 // Ends the rubber banding operation
                 private void EndRubberBanding()
                 {
                    // Stop clipping the cursor
                    ClipCursor(false);
                    rasterImageViewer1.Capture = false;
                    _isRubberBanding = false;
                    if(_isRubberBandingRectangleDrawn)
                       DrawRubberBandRectangle();
                 }
                 // Draws the rubberband rectangle on the viewer
                 private void DrawRubberBandRectangle()
                 {
                    Rectangle rect = FixRectangle(_rubberBandingRectangle);
                    rect.Width++;
                    rect.Height++;
                    rect = rasterImageViewer1.RectangleToScreen(rect);
                    ControlPaint.DrawReversibleFrame(rect, Color.Transparent, FrameStyle.Thick);
                    _isRubberBandingRectangleDrawn = !_isRubberBandingRectangleDrawn;
                 }
                 // Clips the mouse cursor on the dimension of the image in the viewer
                 private void ClipCursor(bool clip)
                 {
                    if(clip)
                    {
                       Rectangle rect = Rectangle.Intersect(FixRectangle(rasterImageViewer1.PhysicalViewRectangle), rasterImageViewer1.ClientRectangle);
                       rect = rasterImageViewer1.RectangleToScreen(rect);
                       Control parent = rasterImageViewer1.Parent;
                       while(parent != null)
                       {
                          rect = Rectangle.Intersect(rect, rasterImageViewer1.Parent.RectangleToScreen(rasterImageViewer1.Parent.ClientRectangle));
                          if(parent is Form)
                          {
                             Form form = parent as Form;
                             if(form.IsMdiChild)
                             {
                                if(form.Owner != null)
                                   rect = Rectangle.Intersect(rect, form.Owner.RectangleToScreen(form.Owner.ClientRectangle));
                                else if(form.Parent != null)
                                   rect = Rectangle.Intersect(rect, form.Parent.RectangleToScreen(form.Parent.ClientRectangle));
                             }
                          }
                          parent = parent.Parent;
                       }
                       rect.Height += 1;
                       rect.Width += 1;
                       _saveClipRectangle = Cursor.Clip;
                       Cursor.Clip = rect;
                    }
                    else
                    {
                       Cursor.Clip = _saveClipRectangle;
                       _saveClipRectangle = Rectangle.Empty;
                    }
                 }
                 // Fox the rectangle coordinates is inverted (its right or bottom value greater than its left or top)
                 private static Rectangle FixRectangle(Rectangle rect)
                 {
                    if(rect.Left > rect.Right)
                       rect = Rectangle.FromLTRB(rect.Right, rect.Top, rect.Left, rect.Bottom);
                    if(rect.Top > rect.Bottom)
                       rect = Rectangle.FromLTRB(rect.Left, rect.Bottom, rect.Right, rect.Top);
                    return rect;
                 }
                      
                 
    
    
        
    
  20. Add the following private method to Form1 to add the zone to the page, recognize and show the results:

    [Visual Basic]

            
    
                 Private Sub AddAndRecognizeZone(ByVal bounds As RectangleF)
                    Dim page As IOcrPage = _ocrDocument.Pages(0)
                    ' First clear any previous zones from the page
                    page.Zones.Clear()
                    ' Add a zone with the given coordinates to the page
                    Dim zone As New OcrZone()
                    zone.Bounds = new LogicalRectangle(bounds.Left, bounds.Top, bounds.Width, bounds.Height, LogicalUnit.Pixel)
                    ' Leave the rest of the zone properties as default
                    page.Zones.Add(zone)
                    ' Recognize the page now and get the results as a string
                    Dim txt As String = page.RecognizeText(Nothing)
                    ' Put the results in the text box
                    textBox1.Text = txt
                 End Sub
                      
                 
    
    
          
    

    [C#]

            
    
                 private void AddAndRecognizeZone(RectangleF bounds)
                 {
                    IOcrPage page = _ocrDocument.Pages[0];
                    
                    // First clear any previous zones from the page
                    page.Zones.Clear();
                
                    // Add a zone with the given coordinates to the page
                    OcrZone zone = new OcrZone();
                    zone.Bounds = new LogicalRectangle(bounds.Left, bounds.Top, bounds.Width, bounds.Height, LogicalUnit.Pixel);
                 
                    // Leave the rest of the zone properties as default
                    page.Zones.Add(zone);
                
                    // Recognize the page now and get the results as a string
                    string txt = page.RecognizeText(null);
                
                    // Put the results in the text box
                    textBox1.Text = txt;
                 }
                      
                 
    
    
          
    

  21. Build and Run the project to test it. After the program starts, you can use the +, - and Enter key to zoom in and out of the page. Use the mouse to draw a zone on the page, once a zone is drawn, the text inside will show up in the text box.

  22. Save this project to use for testing other code samples.

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.