---
title: C#
slug: cisdk-cs
docTags: 
createdAt: 2023-03-12T02:57:33.000Z
---

## Install <font color="#eb144c">Inlite.BarcodeReader.IP</font>  NuGet packages&#x20;

This package installs <font color="#eb144c">`ClearImageNet.70.dll`</font> assembly, which contains two namespaces.

- <font color="#eb144c">`Inlite.ClearImageNet`</font> namespace implements high-level functions **recommended&#x20;**&#x74;o use in new applications.   Code examples use only this namespace.
- <font color="#eb144c">`Inlite.ClearImage`</font> namespace is substantially compatible with the COM API and is specifically designed to ease the migration from ClearImage COM API applications to .NET. It also supports a few low-level functions.  &#x20;

## ClearImage Project Development

Use Visual Studio [C# ClearImage example project](https://barcode-reader.inliteresearch.com/get-ci_example_cs) or follow the code examples below.

### Extend functionality

Combine code examples on this page with methods described in [ClearImage API documentation](http://www.inliteresearch.com/help/ci) in developing your project.  Contact **support\@inliteresearch.com** if you have questions.

### Exception handling

ClearImage .NET API reports errors as an <font color="#eb144c">`Exception`</font> object.&#x20;

```csharp
try {
    //  Call ClearImage .NET API
    }
catch(Exception ex) {
    Console.WriteLine("ERROR: " + ex.Message);
    }
```

## Barcode Reading

### Read 1D  barcodes from a page

This example reads code 39 and code 128 barcodes. Set barcode types used in your application. Setting <font color="#eb144c">`reader.Auto1D=true;`</font> automatically finds barcode type, but it is slower and is not recommended for production. &#x20;


```csharp
using Inlite.ClearImageNet;

void ReadBarcode1D_page(string fileName, int page) {
    try {
        using(BarcodeReader reader = new BarcodeReader()) {
            reader.Code39 = true; reader.Code128 = true;  
            // reader.Auto1D = true;
            Barcode[] barcodes = reader.Read(fileName, page);
            foreach(Barcode barcode in barcodes)
                Console.WriteLine($"Barcode type: {barcode.Type}   Text: {barcode.Text}");
            }
        }
    catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
    }
```

### Read 2D Barcode from a page

This example reads PDF417, DataMatrix, and QR code. 2D barcodes might contain binary data available in <font color="#eb144c">`Barcode.Data`</font> property.

```csharp
using Inlite.ClearImageNet;

void ReadBarcode2D_page(string fileName, int page) {
    try {
        using(BarcodeReader reader = new BarcodeReader()) {
            reader.Pdf417 = true; 
            // reader.DataMatrix = true;
            // reader.QR = true;
            Barcode[] barcodes = reader.Read(fileName, page);
            foreach(Barcode barcode in barcodes) 
                Console.WriteLine($"Barcode type: {barcode.Type}   Text: {barcode.Text}");
            }
        }
    catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
    }
```

### Read Postal Barcodes from a page

This example reads Postal Barcodes from an image file page.  &#x20;

```csharp
using Inlite.ClearImageNet;

void ReadPostal_page(string fileName, int page) {
    try {
        using(BarcodeReader reader = new BarcodeReader()) {
            reader.FourState = true;  //  <= Read postal barcodes
            Barcode[] barcodes = reader.Read(fileName, page);
            foreach(Barcode barcode in barcodes) {
                switch(barcode.Type) {
                    case BarcodeType.UspsIntelligentMail:
                        Console.Write(" US Intelligent Mail"); break;
                    case BarcodeType.BpoPostcode:
                        Console.Write("UK Royal Mail"); break;
                    case BarcodeType.AustralianPost:
                        Console.Write("Australian Mail"); break;
                    case BarcodeType.SingaporePost:
                        Console.Write("Singapore Mail"); break;
                    case BarcodeType.FourState:
                        Console.Write("ForState "); break;
                    }
                Console.WriteLine("   Text: " + barcode.Text);
                }
            }
        }
    catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
    }
```

### Read Driver's License barcode

This example reads the Driver's License barcode from an image file page.  &#x20;

```csharp
using Inlite.ClearImageNet;

void ReadDriverLicBarcode_page(string fileName, int page) {
    try {
        using(BarcodeReader reader = new BarcodeReader()) {
            reader.DrvLicID = true;  
            Barcode[] barcodes = reader.Read(fileName, page);
            foreach(Barcode barcode in barcodes) {
                Inlite.Data.DLDecoder decoder = new Inlite.Data.DLDecoder();
                if(decoder.Decode(text) != "") {
                    Console.WriteLine("last: " + decoder.last);
                    Console.WriteLine("first: " + decoder.first);
                    Console.WriteLine("middle: " + decoder.middle);
                    Console.WriteLine("dob: " + decoder.dob);
                    Console.WriteLine("eyes: " + decoder.eyes);
                    Console.WriteLine("hair: " + decoder.hair);
                    Console.WriteLine("sex: " + decoder.sex);
                    Console.WriteLine("height: " + decoder.height);
                    Console.WriteLine("street: " + decoder.street);
                    Console.WriteLine("city: " + decoder.city);
                    Console.WriteLine("state: " + decoder.state);
                    Console.WriteLine("postal: " + decoder.postal);
                    Console.WriteLine("country: " + decoder.country);
                    Console.WriteLine("id: " + decoder.id);
                    Console.WriteLine("issued: " + decoder.issued);
                    Console.WriteLine("expires: " + decoder.expires);
                    }
                }
            }
        }
    catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
    }
```

### Read from  a multi-page image file

This example reads barcodes from a single-page file or all pages of a multi-page image file.  &#x20;

```csharp
using Inlite.ClearImageNet;

void ReadBarcode1D_file(string fileName) {
    try {
        using(BarcodeReader reader = new BarcodeReader()) {
            reader.Code39 = true; reader.Code128 = true;
            Barcode[] barcodes = reader.Read(fileName); // <== Read from all pages
            foreach(Barcode barcode in barcodes) 
                Console.WriteLine($"Barcode type: {barcode.Type}  Barcode page: {barcode.Page}  Text: {barcode.Text}");
            }
        }
    catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
    }
```

### Read from a stream

This example reads barcodes from a stream. The stream contains the content of a single image file.  For example, if a file is stored in a database. &#x20;

```csharp
using Inlite.ClearImageNet;

void ReadBarcode1D_stream(Stream stream) {
    try {
        using(BarcodeReader reader = new BarcodeReader()) {
            reader.Code39 = true; reader.Code128 = true;
            Barcode[] barcodes = reader.Read(stream); // <== read from a stream
            foreach(Barcode barcode in barcodes) 
                Console.WriteLine($"Barcode type: {barcode.Type}  Barcode page: {barcode.Page}  Text: {barcode.Text}");
            }
        }
    catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
    }
```

### Optimize barcode reading

This [page](docId\:SmK4-LGRtfEx0zacVU2LV) describes methods for improving the recognition rate and/or speed

## Image Processing

### Repair image page

This example uses some of the most popular image-processing functions. In a production application, the specific sequence of functions should be selected based on specific application needs. 

```csharp
using Inlite.ClearImageNet;

static void RepairPage(ImageEditor editor) {
    editor.AutoDeskew();
    editor.AutoRotate();
    editor.AutoCrop(10, 10, 10, 10);     // Crop to 10 pixels on each side
    // editor.AdvancedBinarize();        // Convert to bi-tonal an image with complex background patterns
    editor.ToBitonal();
    editor.BorderExtract(BorderExtractMode.deskewCrop); // Deskew and crop based on black border
    editor.RemovePunchHoles();
    editor.SmoothCharacters();
    editor.CleanNoise(3);               // Clean black noise of 3 pixels
    // editor.CleanNoise    // Clean black and white noise            
    //    (CleanNoiseFlags.black | CleanNoiseFlags.white, 3, 3, 10); 
    editor.ReconstructLines(LineDirection.horzAndVert);
    }

void Repair_page(string fileName, int page, string fileOut) {
    try {
        using(ImageEditor editor = new ImageEditor()) {
            editor.Image.Open(fileName, page);
            // Do image repair
            RepairPage(editor);
            // Save results
            editor.Image.SaveAs(fileOut, Inlite.ClearImage.EFileFormat.ciEXT);
            }
        }
    catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
    }

```

### Repair all images in a multi-page file

This example repairs all images in a multi-page file and saves the result in the <font color="#eb144c">`fileOut`</font> file.

```csharp
using Inlite.ClearImageNet;

private static void _OnEditPage(object sender, EditPageEventArgs e) {
    // e.cancel = true;    // Abort file processing 
    // e.skipPage = true;  // Remove this page from the output
    RepairPage(e.Editor);  // See this method in a single page example
    }

void Repair_file(string fileName, string fileOut) {
    try {
        using(ImageEditor editor = new ImageEditor()) {
            bool ret = editor.Edit(fileName, _OnEditPage, fileOut, ImageFileFormat.outputFileExtension, true);
            }
        }
    catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
    }
```

### Repair all images in a stream

This example repairs all images in an input stream and saves the output stream using a specified file format. Streams contain the content of a single image file. For example, if a file is stored in a database. &#x20;

```csharp
using Inlite.ClearImageNet;

private static void _OnEditPage(object sender, EditPageEventArgs e) {
    RepairPage(e.Editor);  // See this method in a single page example
    }

Stream Repair_stream(Stream stream, ImageFileFormat output_format) {
    try {
        using(ImageEditor editor = new ImageEditor()) {
            MemoryStream msOut = editor.Edit(stream, _OnEditPage, output_format);
            return msOut;
            }
        }
    catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); return null; }
    }
```







