Detect and Extract MRTD - C# .NET 6

This tutorial shows how to detect and extract passport information from a given image in a C# .NET 6 application using the LEADTOOLS SDK.

Summary This tutorial covers how to find and extract passport features from an image in a C# .NET 6 Console application.
Completion Time 20 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 MRTD - 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 packages:

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 MRTD Reader and Processing 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.

using System; 
using System.IO 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Forms.Commands; 
using Leadtools.Ocr; 

Add a new method to the Program class named ExtractMRTD(RasterImage image). Call this method below the call to the LoadImage() method inside the Main() method, as shown below.

Ensure that you replace the string value passed into the LoadImage() method with the file path to your passport image. For the purposes of this tutorial the following image is used: C:\LEADTOOLS22\Resources\Images\mrz_sample.jpg

RasterImage image = LoadImage(@"C:\LEADTOOLS22\Resources\Images\mrz_sample.jpg"); 

Add the code below to the ExtractMRTD() method to process the given image and output the MRTD values if detected.

static void ExtractMRTD(RasterImage image) 
    MRTDReader mrtdReader = new MRTDReader(); 
    // Create the OCR Engine 
    using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
        ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime"); 
        mrtdReader.OcrEngine = ocrEngine; 
        // Process Image 
        // Output values 
        if (mrtdReader.Errors == MRTDErrors.NoError) 
            foreach (var value in mrtdReader.Results) 
                Console.WriteLine(string.Format("Data Element Field: {0}", value.Key.ToString())); 
                Console.WriteLine(string.Format("Data Element Value: {0}", value.Value.ReadableValue)); 
                Console.WriteLine(string.Format("Data Element Code : {0}", value.Value.MrzCharacters)); 
                Console.WriteLine(string.Format("Data Element Valid: {0}", value.Value.IsValid.ToString())); 

Handling Streams

To handle files using memory stream, then insert the following code into the Main() method:

string filename = @"C:\LEADTOOLS22\Resources\Images\mrz_sample.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); 
// RasterImage image = LoadImage(@"C:\LEADTOOLS22\Resources\Images\mrz_sample.jpg"); 
// ExtractMRTD(image); 


This code will replace the existing code in the Main() method under the SetLicense() call. Also note that using System.IO is to 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 detects the MRTD text and displays the information to the console.

Extracted MRTD information displayed to the console.


This tutorial showed how to load an image and run passport recognition using the MRTDReader 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.