Load and Save OCR Zones - WinForms C# .NET 6

This tutorial shows how to load and save OCR zones in a .NET 6 C# WinForms application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use LEADTOOLS OCR SDK technology in a C# WinForms .NET 6 application
Completion Time 30 minutes
Visual Studio Project Download tutorial project (4 KB)
Platform .NET 6 C# Windows WinForms Application
IDE Visual Studio 2022
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License and the Convert Images to Searchable PDF with OCR tutorials, before working on the Load and Save OCR Zones - WinForms C# tutorial.

Create the Project and Verify the LEADTOOLS References

Start with a copy of the project created in the Convert Images to Searchable PDF with OCR tutorial. If the project is not available, follow the steps in that tutorial to create it.

Needed references for this project can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:

NuGet references requires the following NuGet packages and their dependencies:

If local DLL references are used, the following DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\net:

For a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.

Set the License File

The License unlocks needed features, and must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Display OCR Zones Code

With the project created, references added, license set, and code from the Convert Images to Searchable PDF with OCRtutorial added, coding can begin.

In the Solution Explorer, open Form1.cs. Add a new drop-down menu titled &Zones. Add three new menu items to the drop-down titled &Display Zones, &Load Zones, and &Save Zones, respectively. Keep the names of the new items as displayZonesToolStripMenuItem, loadZonesToolStripMenuItem, and saveZonesToolStripMenuItem.

Newly added menu items to the menu strip.

In the designer, double-click on the &Display Zones menu item to add a function that displays the OCR zones in the image viewer. Add the following code to the displayZonesToolStripMenuItem_Click method:

C#
private void displayZonesToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
       if (_ocrPage != null && _ocrPage.Zones != null && _ocrPage.Zones.Count > 0) 
       { 
           Graphics g = _viewer.CreateGraphics(); 
           foreach (OcrZone zone in _ocrPage.Zones) 
           { 
               // Convert bounds coordinates from Image to Control 
               LeadRect zoneLeadRect = _viewer.ConvertRect(_viewer.ActiveItem, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, zone.Bounds); 
               // Draw blue rectangle for each on the viewer 
               g.DrawRectangle(new Pen(Color.Blue), zoneLeadRect.X, zoneLeadRect.Y, zoneLeadRect.Width, zoneLeadRect.Height); 
           } 
 
           MessageBox.Show($"There are {_ocrPage.Zones.Count} OCR Zones"); 
           // Repaint viewer to remove rectangles 
           _viewer.Refresh(); 
       } 
       else 
           MessageBox.Show($"There are no OCR Zones"); 
   } 
   catch (Exception ex) 
   { 
       MessageBox.Show(ex.ToString()); 
   } 
} 

This will use the Graphics object from the ImageViewer to draw a blue rectangle corresponding to each existing OCR recognition zone. This will also display a MessageBox showing the number of zones currently present.

Once OK is pressed on the MessageBox, the code refreshes the ImageViewer and repaints the original image over the drawn rectangles.

Add the Code to Load OCR Zones

In the Solution Explorer, open Form1.cs to bring up the designer. Double-click on the &Load Zones menu item to add a function that loads a Zone file(OZF). Add the following code to the loadZonesToolStripMenuItem_Click method:

C#
private void loadZonesToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
       if (_ocrPage != null) 
       { 
           // Load OCR Zones from OZF file 
           OpenFileDialog loadZoneDlg = new OpenFileDialog(); 
           loadZoneDlg.InitialDirectory = @"C:\LEADTOOLS22\Resources\Images"; 
           loadZoneDlg.Filter = "Zone files (*.ozf)|*.ozf"; 
           if (loadZoneDlg.ShowDialog(this) == DialogResult.OK) 
           { 
               _ocrPage.LoadZones(loadZoneDlg.FileName); 
               MessageBox.Show($"{_ocrPage.Zones.Count} Zones after loading zones from file."); 
           } 
 
       } 
       else 
           MessageBox.Show("Load an Image First"); 
   } 
   catch (Exception ex) 
   { 
       MessageBox.Show(ex.ToString()); 
   } 
} 

Add the Code to Save OCR Zones

In the Solution Explorer, open Form1.cs to bring up the designer. Double-click on the &Save Zones menu item to add a function that will allow you to save the OCR Zones to an OZF file. Add the following code to the saveZonesToolStripMenuItem_Click method:

C#
private void saveZonesToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
    try 
    { 
        if (_ocrPage != null && _ocrPage.Zones != null && _ocrPage.Zones.Count > 0) 
        { 
            SaveFileDialog saveZoneDlg = new SaveFileDialog(); 
            saveZoneDlg.InitialDirectory = @"C:\LEADTOOLS22\Resources\Images"; 
            saveZoneDlg.Filter = "Zone files (*.ozf)|*.ozf"; 
            if (saveZoneDlg.ShowDialog(this) != DialogResult.OK) 
                return; 
            _ocrPage.SaveZones(saveZoneDlg.FileName); 
            MessageBox.Show($"Zones saved to {saveZoneDlg.FileName}"); 
        } 
        else 
            MessageBox.Show($"There are no OCR Zones"); 
    } 
    catch (Exception ex) 
    { 
        MessageBox.Show(ex.ToString()); 
    } 
} 

Add the Code to Clear OCR Zones

A function is needed to clear OCR Zones when a new image is loaded. Add the following code to the openToolStripMenuItem_Click method:

C#
// Remove Old Zones 
if (_ocrPage != null && _ocrPage.Zones != null) 
    _ocrPage.Zones.Clear(); 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application will be able to load, save, and display OCR zones. Using &Save As Searchable PDF after loading an image will automatically create OCR Zones for the loaded image. The zones can be displayed on the ImageViewer using &Display Zones. The Zones can be saved as OZF on disk using &Save Zones, then can be loaded using &Load Zones.

Wrap-up

This tutorial shows how to load and save OCR zones and how to use the IOcrEngine and IOcrPage interfaces, as well as the OcrZone structure.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.