Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
OCR Tutorial - Adding and Painting Zones

Take the following steps to create and run a program that shows how to add/delete and paint the zones in an OCR document. 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 "OcrTutorial1" 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 "\LEAD Technologies\LEADTOOLS 16.5\Bin\DotNet\Win32" folder and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.WinForms.dll
    • Leadtools.Forms.dll
    • Leadtools.Forms.DocumentWriters.dll
    • Leadtools.Forms.Ocr.dll
    • Leadtools.Forms.Ocr.Plus.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. 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 "\LEAD Technologies\LEADTOOLS 16.5\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).

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

    PropertyValue
    DockFill
    InteractiveModePan
    UseDpiTrue

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

    EventHandler
    PostImagePaintrasterImageViewer1_PostImagePaint
    MouseDownrasterImageViewer1_MouseDown
    MouseDoubleClickrasterImageViewer1_MouseDoubleClick

  9. 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 Imports or using section if there are any:

    [Visual Basic]

    
    Imports Leadtools
    Imports Leadtools.WinForms
    Imports Leadtools.Forms
    Imports Leadtools.Forms.Ocr
    Imports Leadtools.Forms.DocumentWriters
         

    [C#]

    
    using Leadtools;
    using Leadtools.WinForms
    using Leadtools.Forms;
    using Leadtools.Forms.Ocr;
    using Leadtools.Forms.DocumentWriters;
         
  10. Add the following private variables to the Form1 class:

    [Visual Basic]

    
    Private _ocrEngine As IOcrEngine
    Private _ocrDocument As IOcrDocument
         

    [C#]

    
    private IOcrEngine _ocrEngine;
    private IOcrDocument _ocrDocument;
         
  11. 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.Unlock(RasterSupportType.OcrPlus, "Replace with your own key here")
       ' Unlock the OCR PDF output support
       RasterSupport.Unlock(RasterSupportType.OcrPlusPdfLeadOutput, "Replace with your own key here")
    
       ' Unlock Document support (For ScaleToGray)
       RasterSupport.Unlock(RasterSupportType.Document, "Replace with your own key here")
    
       ' Initialize the OCR engine
       _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, 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)
    
       ' Auto-zone this page
       page.AutoZone(Nothing)
    
       ' Add an extra zone, this is our user defined one
       Dim zone As New OcrZone()
       zone.Name = "Custom zone"
       zone.Bounds = New LogicalRectangle(10, 10, page.Width - 20, 100, LogicalUnit.Pixel)
       zone.RecognitionModule = OcrZoneRecognitionModule.Auto
       zone.FillMethod = OcrZoneFillMethod.Default
       page.Zones.Add(zone)
    
       ' 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
    
       ' Show this image in the viewer
       RasterImageViewer1.Image = page.GetRasterImage()
    
       Text = "Right click on any zone to remove it from the page, double click anywhere to save the result as a PDF file"
    End Sub
         

    [C#]

    
    public Form1()
    {
       InitializeComponent();
    
       // Unlock the OCR support
       RasterSupport.Unlock(RasterSupportType.OcrPlus, "Replace with your own key here");
       // Unlock the OCR PDF output support
       RasterSupport.Unlock(RasterSupportType.OcrPlusPdfLeadOutput, "Replace with your own key here");
    
       // Unlock Document support (For ScaleToGray)
       RasterSupport.Unlock(RasterSupportType.Document, "Replace with your own key here");
    
       // Initialize the OCR engine
       _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, false);
    
       // Startup the engine
       _ocrEngine.Startup(null, null, null, null);
    
       // Create the OCR document
       _ocrDocument = _ocrEngine.DocumentManager.CreateDocument();
    
       // Load an image into the engine
       string fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif";
       IOcrPage page = _ocrDocument.Pages.AddPage(fileName, null);
    
       // Auto-zone this page
       page.AutoZone(null);
    
       // Add an extra zone, this is our user defined one
       OcrZone zone = new OcrZone();
       zone.Name = "Custom zone";
       zone.Bounds = new LogicalRectangle(10, 10, page.Width - 20, 100, LogicalUnit.Pixel);
       zone.RecognitionModule = OcrZoneRecognitionModule.Auto;
       zone.FillMethod = OcrZoneFillMethod.Default;
       page.Zones.Add(zone);
    
       // 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;
    
       // Show this image in the viewer
       rasterImageViewer1.Image = page.GetRasterImage();
    
       Text = "Right click on any zone to remove it from the page, double click anywhere to save the result as a PDF file";
    }
         
  12. 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 OCR document
       _ocrDocument.Dispose()
    
       ' Shutdown and dispose the engine
       _ocrEngine.Dispose()
    
       MyBase.OnFormClosed(e)
    End Sub
         

    [C#]

    
    protected override void OnFormClosed(FormClosedEventArgs e)
    {
       // Destroy the OCR document
       _ocrDocument.Dispose();
    
       // Shutdown and dispose the engine
       _ocrEngine.Dispose();
    
       base.OnFormClosed(e);
    }
         
  13. Add the following code to rasterImageViewer1PostImagePaint event to draw the page zones on top of the image in the viewer:

    [Visual Basic]

    
    Private Sub rasterImageViewer1_PostImagePaint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles rasterImageViewer1.PostImagePaint
       ' Draw the zones
       Dim page As IOcrPage = _ocrDocument.Pages(0)
       For Each zone As OcrZone In page.Zones
          ' The zone boundaries are in logical coordinates, convert them to physical
          ' We used UseDpi in the viewer, so use the GetTransformWithDpi method of RasterImageViewer
          Using matrix As System.Drawing.Drawing2D.Matrix = RasterImageViewer1.GetTransformWithDpi()
             ' The Transformer class has helper methods to convert
             ' Rectangles from logical to physical and back
             Dim t As New Transformer(matrix)
    
             Dim zoneBounds As LogicalRectangle = zone.Bounds
    
             ' The zone bounds is a logical rectangle, it may be in units other than pixels
             ' Convert to pixels
             Dim bounds As RectangleF = zoneBounds.ToRectangleF(page.DpiX, page.DpiY)
    
             ' Convert it to physical coordinates
             bounds = t.RectangleToPhysical(bounds)
    
             ' If this is our custom zone, draw its border a red pen, else use a blue pen
             If (zone.Name = "Custom zone") Then
                e.Graphics.DrawRectangle(Pens.Red, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1)
             Else
                e.Graphics.DrawRectangle(Pens.Blue, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1)
             End If
          End Using
       Next
    End Sub
         

    [C#]

    
    private void rasterImageViewer1_PostImagePaint(object sender, PaintEventArgs e)
    {
       // Draw the zones
       IOcrPage page = _ocrDocument.Pages[0];
       foreach(OcrZone zone in page.Zones)
       {
          // The zone boundaries are in logical coordinates, convert them to physical
          // We used UseDpi in the viewer, so use the GetTransformWithDpi method of RasterImageViewer
          using(System.Drawing.Drawing2D.Matrix matrix = rasterImageViewer1.GetTransformWithDpi())
          {
             // The Transformer class has helper methods to convert
             // Rectangles from logical to physical and back
             Transformer t = new Transformer(matrix);
    
             LogicalRectangle zoneBounds = zone.Bounds;
    
             // The zone bounds is a logical rectangle, it may be in units other than pixels
             // Convert to pixels
             RectangleF bounds = zoneBounds.ToRectangleF(page.DpiX, page.DpiY);
    
             // Convert it to physical coordinates
             bounds = t.RectangleToPhysical(bounds);
    
             // If this is our custom zone, draw its border a red pen, else use a blue pen
             if(zone.Name == "Custom zone")
                e.Graphics.DrawRectangle(Pens.Red, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
             else
                e.Graphics.DrawRectangle(Pens.Blue, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
          }
       }
    }
         
  14. Add the following code to rasterImageViewer1MouseDown event to delete page zones on right button clicks:

    [Visual Basic]

    
    Private Sub rasterImageViewer1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RasterImageViewer1.MouseDown
       ' Check if this is a right button click
       If (e.Button <> MouseButtons.Right) Then
          Return
       End If
    
       ' Check if the mouse click is on a zone
    
       Dim pt As New PointF(e.X, e.Y)
    
       ' First, convert this point from physical into logical coordinates
       Using matrix As System.Drawing.Drawing2D.Matrix = RasterImageViewer1.GetTransformWithDpi()
          ' The Transformer class has helper methods to convert
          ' Rectangles from logical to physical and back
          Dim t As New Transformer(matrix)
          pt = t.PointToLogical(pt)
       End Using
    
       ' Use the HitTestZone method to find the zone under the mouse button
       Dim page As IOcrPage = _ocrDocument.Pages(0)
       Dim zoneIndex As Integer = page.HitTestZone(New LogicalPoint(pt))
    
       If (zoneIndex <> -1) Then
          ' Remove this zone
          page.Zones.RemoveAt(zoneIndex)
    
          ' Re-paint the viewer to show the zones left
          RasterImageViewer1.Invalidate()
          RasterImageViewer1.Update()
    
          ' If no zones are left, show a message
          If (page.Zones.Count = 0) Then
             MessageBox.Show(Me, "No zones left in the page, saving this document to PDF is disabled now")
          End If
       End If
    End Sub
         

    [C#]

    
    private void rasterImageViewer1_MouseDown(object sender, MouseEventArgs e)
    {
       // Check if this is a right button click
       if(e.Button != MouseButtons.Right)
          return;
    
       // Check if the mouse click is on a zone
    
       PointF pt = new PointF(e.X, e.Y);
    
       // First, convert this point from physical into logical coordinates
       using(System.Drawing.Drawing2D.Matrix matrix = rasterImageViewer1.GetTransformWithDpi())
       {
          // The Transformer class has helper methods to convert
          // Rectangles from logical to physical and back
          Transformer t = new Transformer(matrix);
          pt = t.PointToLogical(pt);
       }
    
       // Use the HitTestZone method to find the zone under the mouse button
       IOcrPage page = _ocrDocument.Pages[0];
       int zoneIndex = page.HitTestZone(new LogicalPoint(pt));
    
       if(zoneIndex != -1)
       {
          // Remove this zone
          page.Zones.RemoveAt(zoneIndex);
    
          // Re-paint the viewer to show the zones left
          rasterImageViewer1.Invalidate();
          rasterImageViewer1.Update();
    
          // If no zones are left, show a message
          if(page.Zones.Count == 0)
             MessageBox.Show(this, "No zones left in the page, saving this document to PDF is disabled now");
       }
    }
         
  15. Add the following code to rasterImageViewer1MouseDoubleClick event to save the page as PDF:

    [Visual Basic]

    
    Private Sub RasterImageViewer1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RasterImageViewer1.MouseDoubleClick
       ' Check if we have any zones in the page
       Dim page As IOcrPage = _ocrDocument.Pages(0)
       If (page.Zones.Count > 0) Then
          ' Yes, recognize and save as PDF
          Dim pdfFileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.pdf"
          page.Recognize(Nothing)
          _ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, Nothing)
    
          ' Show the document
          System.Diagnostics.Process.Start(pdfFileName)
       Else
          MessageBox.Show(Me, "No zones left in the page, saving this document to PDF is disabled")
       End If
    End Sub
         

    [C#]

    
    private void rasterImageViewer1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
       // Check if we have any zones in the page
       IOcrPage page = _ocrDocument.Pages[0];
       if(page.Zones.Count > 0)
       {
          // Yes, recognize and save as PDF
          string pdfFileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.pdf";
          page.Recognize(null);
          _ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, null);
    
          // Show the document
          System.Diagnostics.Process.Start(pdfFileName);
       }
       else
          MessageBox.Show(this, "No zones left in the page, saving this document to PDF is disabled");
    }
         
  16. Build and Run the project to test it. After the program starts, you can use the left mouse button to pan the image, right click on any zone to remove it and double click anywhere on the page to save it as PDF.

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

See Also