This tutorial shows how to create a C# WinForms application that uses the LEADTOOLS SDK to load and save OCR zones.
Overview | |
---|---|
Summary | This tutorial covers how to use LEADTOOLS OCR SDK technology in a C# WinForms application |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (11 KB) |
Platform | C# Windows WinForms Application |
IDE | Visual Studio 2017, 2019 |
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.Ocr
Leadtools.Viewer.Controls.WinForms
If local DLL references are used, the following DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Codecs.Tif.dll
Leadtools.Codecs.Fax.dll
Leadtools.Controls.WinForms.dll
Leadtools.Document.Writer.dll
Leadtools.Ocr.dll
Leadtools.Ocr.LEADEngine.dll
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
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 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
.
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 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.
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 file
OpenFileDialog loadZoneDlg = new OpenFileDialog();
loadZoneDlg.InitialDirectory = @"C:\LEADTOOLS21\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());
}
}
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:\LEADTOOLS21\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());
}
}
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 Zones
if (_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.