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 (9 KB) |
| Platform | .NET 6 C# Windows WinForms Application |
| IDE | Visual Studio 2022 |
| Development License | Download LEADTOOLS |
| Try it in another language |
|
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.
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:
Leadtools.OcrLeadtools.Viewer.Controls.WinFormsIf local DLL references are used, the following DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net:
Leadtools.dllLeadtools.Codecs.dllLeadtools.Controls.WinForms.dllLeadtools.Core.dllLeadtools.Document.Writer.dllLeadtools.Ocr.dllLeadtools.Ocr.LEADEngine.dllFor a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.
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.
With the project created, references added, license set, and code from the Convert Images to Searchable PDF with OCR tutorial 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.

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:
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 ControlLeadRect zoneLeadRect = _viewer.ConvertRect(_viewer.ActiveItem, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, zone.Bounds);// Draw blue rectangle for each on the viewerg.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();}elseMessageBox.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.
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:
private void loadZonesToolStripMenuItem_Click(object sender, EventArgs e){try{if (_ocrPage != null){// Load OCR Zones from OZF fileOpenFileDialog loadZoneDlg = new();loadZoneDlg.InitialDirectory = @"C:\LEADTOOLS23\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.");}}elseMessageBox.Show("Load an Image First");}catch (Exception ex){MessageBox.Show(ex.ToString());}}
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:
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:\LEADTOOLS23\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}");}elseMessageBox.Show($"There are no OCR Zones");}catch (Exception ex){MessageBox.Show(ex.ToString());}}
A function is needed to clear OCR Zones when a new image is loaded. Add the following code to the openToolStripMenuItem_Click method:
// Remove Old Zonesif (_ocrPage != null && _ocrPage.Zones != null)_ocrPage.Zones.Clear();
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.
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.