Load and Save OCR Zones - C# .NET 6

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

Overview  
Summary This tutorial covers how to work with OCR zones in a C# .NET 6 Console application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or higher
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 tutorial, before working on the Load and Save OCR Zones - C# .NET 6 tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If the project is not available, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added via NuGet packages.

This tutorial requires the following NuGet package:

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

Set the License File

The License unlocks the features needed for the project. It 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:

Add the OCR Zones Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs.

C#
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 

Add a new method to the Program class named LoadandSaveOcrZones(). Call the LoadandSaveOcrZones() method inside the Main() method below the set license code, as shown below.

C#
static void Main(string[] args) 
{ 
    if (!InitLEAD()) 
        Console.WriteLine("Error setting license"); 
    else 
        Console.WriteLine("License file set successfully"); 
 
    LoadandSaveOcrZones(); 
} 

Add the code below to the LoadandSaveOcrZones() method to load OCR zones to an IOcrPage, output the OCR zone properties to the console, add a new OCR zone programmatically, and then export the OCR zones to file.

C#
static void LoadandSaveOcrZones() 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
 
      ocrEngine.Startup(codecs, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime"); 
      string zonesFile = @"PATH TO OCR ZONES FILE"; 
      string imageFile = @"SAMPLE IMAGE TO CREATE OCR PAGE"; 
      string zonesOutFile = @"PATH TO SAVE OCR ZONES FILE"; 
 
      // Load the TIFF file as image 
      RasterImage image = ocrEngine.RasterCodecsInstance.Load(imageFile, 1); 
 
      using (IOcrPage ocrPage = ocrEngine.CreatePage(image, OcrImageSharingMode.None)) 
      { 
         Console.WriteLine($"{ocrPage.Zones.Count} Zones after IOcrPage creation.\n"); 
         ocrPage.LoadZones(zonesFile); 
 
         Console.WriteLine($"{ocrPage.Zones.Count} Zones after loading zones from file.\n"); 
         foreach (OcrZone ocrZone in ocrPage.Zones) 
         { 
            int index = ocrPage.Zones.IndexOf(ocrZone); 
            Console.WriteLine("Zone index: {0}", index); 
            Console.WriteLine("  Id                  {0}", ocrZone.Id); 
            Console.WriteLine("  Bounds              {0}", ocrZone.Bounds); 
            Console.WriteLine("  ZoneType            {0}", ocrZone.ZoneType); 
            Console.WriteLine("  CharacterFilters:   {0}", ocrZone.CharacterFilters); 
            Console.WriteLine("----------------------------------"); 
         } 
         // Add an extra zone, this is our user-defined one 
         OcrZone zone = new OcrZone(); 
         zone.Name = "Custom zone"; 
         zone.ZoneType = OcrZoneType.Text; 
         zone.Bounds = new LeadRect(10, 10, ocrPage.Width - 20, 100); 
         ocrPage.Zones.Add(zone); 
 
         Console.WriteLine($"{ocrPage.Zones.Count} Zones after adding zone manually.\n"); 
 
         ocrPage.SaveZones(zonesOutFile); 
 
         Console.WriteLine($"Zones successfully saved to {zonesOutFile}"); 
         Console.WriteLine("Press any key to exit..."); 
         Console.ReadKey(true); 
 
      } 
   } 
} 

Handling Streams

To load the image from a memory stream, use the following code instead of loading directly from the file:

C#
RasterImage image; 
byte[] imageData = File.ReadAllBytes(imageFile); 
using (MemoryStream imageStream = new MemoryStream(imageData)) 
   image = ocrEngine.RasterCodecsInstance.Load(imageStream); 

To load the OCR zones file from memory stream, replace ocrPage.LoadZones(zonesFile) with the following code:

C#
byte[] zonesData = File.ReadAllBytes(zonesFile); 
using (MemoryStream zonesStream = new MemoryStream(zonesData)) 
   ocrPage.LoadZones(zonesStream); 

To save the OCR zones to a memory stream, use the code below instead of ocrPage.SaveZones(zonesOutFile)

C#
using (MemoryStream zonesOutStream = new MemoryStream()) 
   ocrPage.SaveZones(zonesOutStream); 

Run the Project

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

If the steps were followed correctly, the console appears and the application creates an IOcrPage object, loads OCR zones from the specified file, adds a new OcrZone to the IOcrPage, and then exports the zones in XML to the specified file path.

Wrap-up

This tutorial showed how to work with OCR zones using the OCRZone structure. Also, we covered how to use the IOcrEngine and IOcrPage interfaces.

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.