Preprocess an Image for OCR - WinForms C#

This tutorial shows how to create a C# Windows WinForms application that uses the LEADTOOLS SDK to preprocess images for OCR Recognition.

Overview  
Summary This tutorial covers how to use LEADTOOLS Image Processing SDK technology in a C# WinForms application
Completion Time 30 minutes
Visual Studio Project Download tutorial project (10 KB)
Platform C# Windows WinForms Application
IDE Visual Studio 2017, 2019
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 and the Convert Images to Searchable PDF with OCR tutorials before working on the Preprocess an Image for OCR - WinForms C# tutorial.

Create the Project and Verify the LEADTOOLS References

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.

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 NuGet references are used, this tutorial requires the following NuGet packages:

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

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:

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.

Add the Image Preprocessing and OCR code

With the project created, the references added, the license set, the load image code added, and the ImageViewer and IOcrEngine initialized, coding can begin.

In the Solution Explorer, open Form1.cs. Add a new drop down menu titled &Preprocess. Add three menu items, to the new drop down menu, titled &Deskew, &Invert, and &Rotate. Keep the names of the new items as deskewToolStripMenuItem, invertToolStripMenuItem, and rotateToolStripMenuItem.

Double-click on the &Deskew menu item, to add a new event handler. Add the following code to the deskewToolStripMenuItem_Click method:

C#
private void deskewToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
       if (_viewer.Image != null) 
       { 
           // Create OCR Page from Image 
           if (_ocrPage == null) 
               _ocrPage = _ocrEngine.CreatePage(_viewer.Image, OcrImageSharingMode.AutoDispose); 
 
           _ocrPage.AutoPreprocess(OcrAutoPreprocessPageCommand.Deskew, null); 
       } 
       else 
           MessageBox.Show("Load an Image First"); 
   } 
   catch (Exception ex) 
   { 
       MessageBox.Show(ex.ToString()); 
   } 
} 

Navigate back to the designer and double-click on the &Invert menu item. Add the following code to the invertToolStripMenuItem_Click method:

C#
private void invertToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
       if (_viewer.Image != null) 
       { 
           // Create OCR Page from Image 
           if (_ocrPage == null) 
               _ocrPage = _ocrEngine.CreatePage(_viewer.Image, OcrImageSharingMode.AutoDispose); 
 
           _ocrPage.AutoPreprocess(OcrAutoPreprocessPageCommand.Invert, null); 
       } 
       else 
           MessageBox.Show("Load an Image First"); 
   } 
   catch (Exception ex) 
   { 
       MessageBox.Show(ex.ToString()); 
   } 
} 

Finally, go back to the designer and double-click on the &Rotate menu item. Add the following code to the rotateToolStripMenuItem_Click method:

C#
private void rotateToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
       if (_viewer.Image != null) 
       { 
           // Create OCR Page from Image 
           if (_ocrPage == null) 
               _ocrPage = _ocrEngine.CreatePage(_viewer.Image, OcrImageSharingMode.AutoDispose); 
 
           _ocrPage.AutoPreprocess(OcrAutoPreprocessPageCommand.Rotate, null); 
       } 
       else 
           MessageBox.Show("Load an Image First"); 
   } 
   catch (Exception ex) 
   { 
       MessageBox.Show(ex.ToString()); 
   } 
} 

Run the Project

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

If the steps were followed correctly, the application will have the ability to apply deskew, invert, and rotate preprocessing to the loaded image. This can be seen by loading the CLEAN.TIF sample (from C:\LEADTOOLS21\Resources\Images) and applying preprocessing using the application.

The result after preprocessing can be saved as PDF.

Wrap-up

This tutorial showed how to clean up image using image preprocessing. Also, it covered how to use the IOcrPage, IOcrDocument, and IOcrEngine interfaces.

See Also

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