Detect and Extract MICR - C# .NET 6

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

Overview  
Summary This tutorial covers how to run MICR detection and output the recognition results in a C# .NET 6 Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project and loading an image by reviewing the Add References and Set a License and Load and Save Images tutorials, before working on the Detect and Extract MICR - C# .NET 6 tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Load and Save Images tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added via NuGet packages.

This tutorial requires the following NuGet package:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

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:

Add the MICR Detection and Bank Check Reader Code

With the project created, the references added, the license set, and the load image code added, coding can begin. The image save code is not necessary for this tutorial, so that code can be commented out or deleted.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs.

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

Add a new method to the Program class named RunMICRDetectionRecognition(RasterImage image). Call the RunMICRDetectionRecognition() method inside the Main() method below the call to the LoadImage() method, as shown below. The method's parameter will be the RasterImage loaded in the LoadImage() method. For the purposes of this tutorial the sample check images in the file paths below are used.

C#
static void Main(string[] args) 
{ 
    if (!SetLicense()) 
        Console.WriteLine("Error setting license"); 
    else 
        Console.WriteLine("License file set successfully"); 
 
    // CMC7 sample check image, uncomment if wanting to test CMC7 
    //RasterImage image = LoadImage(@"C:\LEADTOOLS22\Resources\Images\cmc7.jpg"); 
    // E13b sample check image 
    RasterImage image = LoadImage(@"C:\LEADTOOLS22\Resources\Images\bankcheck.jpg"); 
    RunMICRDetectionRecognition(image); 
} 

Add the code below to the RunMICRDetectionRecognition() method to run MICR detection, process the results, and display them to the console.

C#
static void RunMICRDetectionRecognition(RasterImage image) 
{ 
    using (RasterCodecs codecs = new RasterCodecs()) 
    { 
        StringBuilder sb = new StringBuilder(); 
 
        BankCheckReader micrReader = new BankCheckReader(); 
 
        IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD); 
        ocrEngine.Startup(codecs, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime"); 
        micrReader.OcrEngine = ocrEngine; 
        // MICR Code Detection searches for E13b MICR font type 
        MICRCodeDetectionCommand e13bCmd = new MICRCodeDetectionCommand 
        { 
            SearchingZone = new LeadRect(0, 0, image.Width, image.Height) 
        }; 
        e13bCmd.Run(image); 
 
        // Run CMC7 Detection 
        CMC7CodeDetectionCommand cmc7Cmd = new CMC7CodeDetectionCommand(); 
        cmc7Cmd.Run(image); 
        // If E13b MICR code found 
        if (e13bCmd.MICRZone != LeadRect.Empty) 
        { 
            micrReader.MicrFontType = BankCheckMicrFontType.E13b; 
            micrReader.ProcessImage(image); 
 
            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"); 
                } 
            } 
            Console.WriteLine(sb.ToString()); 
            Console.ReadLine(); 
        } 
        // If CMC7 MICR code found 
        else if (cmc7Cmd.CMC7Zone != LeadRect.Empty) 
        { 
            micrReader.MicrFontType = BankCheckMicrFontType.Cmc7; 
            micrReader.ProcessImage(image); 
 
            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"); 
                } 
            } 
            Console.WriteLine(sb.ToString()); 
        } 
        else 
        { 
            Console.WriteLine("No MICR text detected!"); 
        } 
        ocrEngine.Shutdown(); 
    } 
} 

Note

The code snippet above supports functionality for gathering MICR information from an E13b check and a CMC7 check.

Handling Streams

If you would like to detect and extract the data using memory stream, then add the following code to the Main(string[] args) method:

C#
string filename = @"C:\LEADTOOLS22\Resources\Images\bankcheck.jpg"; 
byte[] buffer = File.ReadAllBytes(filename); 
using (RasterCodecs codecs = new RasterCodecs()) 
{ 
    using(MemoryStream ms = new MemoryStream(buffer)) 
    { 
        ms.Position = 0; 
        RasterImage image = codecs.Load(ms); 
        RunMICRDetectionRecognition(image); 
    } 
} 
// RasterImage image = LoadImage(@"C:\LEADTOOLS22\Resources\Images\bankcheck.jpg"); 
// RunMICRDetectionRecognition(image); 

Note

This code will replace the existing code in the Main(string[] args) method under the SetLicense call as seen in the commented out code. Also, note that using System.IO; must be included in the using block.

Run the Project

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

If the steps were followed correctly, the console appears and the application runs MICR detection and displays to the console the check image's MICR information.

Screenshot of the console displaying the check image's MICR information.

Wrap-up

This tutorial showed how to run MICR detection using the MICRCodeDetectionCommand class and output the recognition results to the console using the BankCheckReader class.

See Also

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

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