This tutorial shows how to use the LEAD OCR Engine to OCR an image and display the recognized words to the user in a C# .NET 6 WinForms application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to OCR an image and display the recognized text in a C# .NET 6 WinForms application. |
Completion Time | 20 minutes |
Visual Studio Project | Download tutorial project (4 KB) |
Platform | C# .NET 6 WinForms Application |
IDE | Visual Studio 2022 |
Development License | Download LEADTOOLS |
Get familiar with the basic steps of creating a project and loading an image in the Image Viewer by reviewing the Add References and Set a License and Display Images in an Image Viewer tutorials, before working on the OCR an Image and Display Results tutorial.
Start with a copy of the project created in the Display Images in an Image Viewer tutorial. If the project is not available, follow the steps in that tutorial to create it. The code for saving the loaded image as JPEG from that tutorial is not needed in this tutorial, so it can be omitted.
The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:
If using NuGet references, this tutorial requires the following NuGet packages and their dependencies:
Leadtools.Ocr
Leadtools.Viewer.Controls.WinForms
If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Controls.WinForms.dll
Leadtools.Core.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 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:
In the Solution Explorer, open Form1.cs
. In the designer right-click and select View Code, or press F7, to bring up the code behind the form. Ensure that the following using statements and global variables are added.
using Leadtools;
using Leadtools.Controls;
using Leadtools.Codecs;
using Leadtools.Ocr;
using Leadtools.Ocr.LEADEngine;
using System.Text;
private ImageViewer _viewer;
private IOcrEngine _engine;
Add code inside the Form1_Load
event handler to initialize the IOcrEngine
and set the image zoom to fit always inside the field of view.
private void Form1_Load(object sender, EventArgs e)
{
_viewer = new ImageViewer();
_viewer.Dock = DockStyle.Fill;
_viewer.BackColor = Color.DarkGray;
Controls.Add(_viewer);
_viewer.BringToFront();
_viewer.Zoom(ControlSizeMode.FitAlways, 1.0, _viewer.DefaultZoomOrigin);
_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD);
_engine.Startup(null, null, null, @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime");
}
In the Solution Explorer, open Form1.cs
. In the designer, add a new dropdown MenuItem named OCR next menu in the File dropdown. Add a new MenuItem to the OCR dropdown named OCR Image. You can leave the new item's name as oCRImageToolStripMenuItem
. Double-click the new OCR Image menu item to create its click event handler. Add the code below to the new event handler.
private void oCRImageToolStripMenuItem_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
if (_viewer.Image != null)
{
IOcrPage _ocrPage = _engine.CreatePage(_viewer.Image, OcrImageSharingMode.AutoDispose);
_ocrPage.Recognize(null);
for (int i = 0; i < _ocrPage.Zones.Count; i++)
{
string zoneText = _ocrPage.GetText(i);
sb.Append(zoneText);
sb.Append("\n");
}
MessageBox.Show(sb.ToString());
}
else
{
MessageBox.Show("No Image in the ImageViewer...");
}
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application runs and is able to load and display any image that is supported by the above codecs filters. When OCR Image is pressed, the application recognizes the text in the loaded image (OCR1.TIF, for example) and displays the recognized text in a WinForms MessageBox.
This tutorial showed how to create a simple Windows Forms OCR application that initializes the LEAD OCR Engine, displays a specified input file, and displays the recognized text.