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 (5 KB) |
Platform | 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 Preprocess an Image for OCR - 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.
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:
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.Codecs.Cmp.dll
Leadtools.Codecs.Fax.dll
Leadtools.Codecs.Tif.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 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.
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:
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:
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:
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 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:\LEADTOOLS23\Resources\Images) and applying preprocessing using the application.
The result after preprocessing can be saved as PDF.
This tutorial showed how to clean up image using image preprocessing. Also, it covered how to use the IOcrPage
, IOcrDocument
, and IOcrEngine
interfaces.