OCR an Image and Display Results - WinForms C# .NET 6

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

Required Knowledge

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.

Create the Project and Add LEADTOOLS References

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:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net:

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

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:

Initialize the IOcrEngine Code

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.

C#
using Leadtools; 
using Leadtools.Controls; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 
using Leadtools.Ocr.LEADEngine; 
using System.Text; 
C#
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.

C#
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"); 
} 

Add the OCR Image Code

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.

C#
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

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.

Wrap-up

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.

See Also

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


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