Detect and Extract MICR - WinForms C# .NET 6

This tutorial shows how to perform MICR detection and recognition in a .NET 6 C# Windows WinForms application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use LEADTOOLS MICR SDK technology in a C# Windows WinForms application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (10 KB)
Platform .NET 6 C# Windows WinForms Application
IDE Visual Studio 2022
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 Display Images in an Image Viewer tutorials, before working on the Detect and Extract MICR - WinForms C# 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 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 package:

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:

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 MICR Detection and Bank Check Reader Code

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

In Solution Explorer, open Form1.cs to bring up the designer. Add a new drop down menu titled &MICR Detection Recognition. Now add two menu items to the new drop down menu titled &MICR Code Detection and &CMC7 Code Detection. Keep the names of the new items as deskewToolStripMenuItem and rotateToolStripMenuItem.

Double-click on the &MICR Code Detection menu item to create its click event handler.

Ensure you have the below using statements in the using block at the top.

C#
using System.Text; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Controls; 
using Leadtools.Ocr; 
using Leadtools.Forms.Commands; 
using Leadtools.ImageProcessing.Core; 

Add the below global variables:

C#
private IOcrEngine _ocrEngine; 
private IOcrPage _ocrPage; 

Add the below lines of code to the Form1_Load event to initialize and startup the IOcrEngine. Creation of the Form1_Load event is covered in the Display Images in an Image Viewer tutorial.

C#
// Initialize the OCR engine  
_ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD); 
// Start up the engine  
_ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime"); 

Add the following line of code to the bottom of the openToolStripMenuItem_Click event handler to create an IOcrPage instance from the loaded image. Creation of the openToolStripMenuItem_Click event is covered in the Display Images in an Image Viewer tutorial.

C#
// Create IOcrPage from loaded image 
_ocrPage = _ocrEngine.CreatePage(_viewer.Image, OcrImageSharingMode.AutoDispose); 

Now add the below code to the mICRCodeDetectionToolStripMenuItem_Click event handler to run E13B MICR dectection and extraction on the loaded image.

C#
private void mICRCodeDetectionToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
      if (_ocrPage != null) 
      { 
         StringBuilder sb = new(); 
         BankCheckReader micrReader = new() 
         { 
            OcrEngine = _ocrEngine 
         }; 
         // MICR Code Detection searches for E13b MICR font type  
         MICRCodeDetectionCommand e13bCmd = new() 
         { 
            SearchingZone = new LeadRect(0, 0, _ocrPage.GetRasterImage().Width, _ocrPage.GetRasterImage().Height) 
         }; 
         e13bCmd.Run(_ocrPage.GetRasterImage()); 
 
         // If E13b MICR code found  
         if (e13bCmd.MICRZone != LeadRect.Empty) 
         { 
            micrReader.MicrFontType = BankCheckMicrFontType.E13b; 
            micrReader.ProcessImage(_ocrPage.GetRasterImage()); 
 
            Graphics g = _viewer.CreateGraphics(); 
            foreach (var value in micrReader.Results) 
            { 
               if (value.Key != "Signature") 
               { 
                  sb.Append("\n"); 
                  sb.Append($"Field Name: {value.Key}\n"); 
                  sb.Append($"Field Value: {value.Value.Text}\n"); 
 
                  // Convert bounds coordinates from Image to Control  
                  LeadRect micrLeadRect = _viewer.ConvertRect(_viewer.ActiveItem, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, value.Value.Bounds); 
                  // Draw blue rectangle for each field on the viewer  
                  g.DrawRectangle(new Pen(Color.Blue), micrLeadRect.X, micrLeadRect.Y, micrLeadRect.Width, micrLeadRect.Height); 
               } 
            } 
            MessageBox.Show(sb.ToString()); 
            // Repaint viewer to remove rectangles  
            _viewer.Refresh(); 
         } 
         else 
            MessageBox.Show("No MICR Code Detected"); 
      } 
      else 
         MessageBox.Show("Load an Image First"); 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 

Navigate back to the designer in the Solution Explorer. Double-click on the &CMC7 Code Detection menu item and add the following code to the cMC7CodeDetectionToolStripMenuItem_Click event handler:

C#
private void cMC7CodeDetectionToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
      if (_ocrPage != null) 
      { 
         StringBuilder sb = new(); 
         BankCheckReader micrReader = new() 
         { 
            OcrEngine = _ocrEngine 
         }; 
 
         // Run CMC7 Detection  
         CMC7CodeDetectionCommand cmc7Cmd = new(); 
         cmc7Cmd.Run(_ocrPage.GetRasterImage()); 
 
         if (cmc7Cmd.CMC7Zone != LeadRect.Empty) 
         { 
            micrReader.MicrFontType = BankCheckMicrFontType.Cmc7; 
            micrReader.ProcessImage(_ocrPage.GetRasterImage()); 
 
            Graphics g = _viewer.CreateGraphics(); 
            foreach (var value in micrReader.Results) 
            { 
               if (value.Key != "Signature") 
               { 
                  sb.Append("\n"); 
                  sb.Append($"Field Name: {value.Key}\n"); 
                  sb.Append($"Field Value: {value.Value.Text}\n"); 
 
                  // Convert bounds coordinates from Image to Control  
                  LeadRect micrLeadRect = _viewer.ConvertRect(_viewer.ActiveItem, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, value.Value.Bounds); 
                  // Draw blue rectangle for each field on the viewer  
                  g.DrawRectangle(new Pen(Color.Blue), micrLeadRect.X, micrLeadRect.Y, micrLeadRect.Width, micrLeadRect.Height); 
               } 
            } 
            MessageBox.Show(sb.ToString()); 
            // Repaint viewer to remove rectangles  
            _viewer.Refresh(); 
         } 
         else 
            MessageBox.Show("No CMC7 Code Detected"); 
      } 
      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 is able to run MICR and CMC7 Code detection on a loaded image and display the check image's MICR information. This can be seen using the BANKCHECK.JPG and CMC7.JPG samples from: C:\LEADTOOLS23\Resources\Images

Wrap-up

This tutorial showed how to use the IOcrEngine interface, MICRCodeDetectionCommand class, and BankCheckReader class.

See Also

Help Version 23.0.2024.3.11
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.