IronBarCode Could Not Recognize MSI Barcode

Problem

When creating an MSI barcode using the IronBarCode library, there are instances where the BarcodeReader.Read method is unable to read the generated MSI barcode, resulting in an empty scan and the expected value from the barcode.

Solution

To ensure that the BarcodeReader.Read can read the MSI barcode, we must provide the secondary optional BarcodeReaderOptions parameter for the Read method. Within the BarcodeReaderOptions, we explicitly specify the barcode type that we are trying to read by assigning the value ExpectedBarcodeTypes. This way, the Read method would recognize the MSI barcode generated from IronBarCode as well as any external MSI barcodes.

Below is a quick example of how to apply BarcodeReaderOptions to BarcodeReader.Read.

Code Example

// Creating MSI Barcode with the value "12345"
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.MSI);

// Save barcode as image
myBarcode.SaveAsImage("msi.png");

// Reading MSI
var barcodeReaderOptions = new BarcodeReaderOptions
{
     // Assigning BarcodeEncoding.MSI to ExpectBarcodeType to ensure that IronBarcode expects MSI type barcodes specifically 
  ExpectBarcodeTypes = BarcodeEncoding.MSI,
};

// Read barcode with additional barcodeReaderOptions from above
var barcodeResults = BarcodeReader.Read("msi.png", barcodeReaderOptions);

// Using a for loop and print out the result 
foreach (BarcodeResult result in barcodeResults)
{
  Console.WriteLine(result.Text);
    // Output: 12345 
}
// Creating MSI Barcode with the value "12345"
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.MSI);

// Save barcode as image
myBarcode.SaveAsImage("msi.png");

// Reading MSI
var barcodeReaderOptions = new BarcodeReaderOptions
{
     // Assigning BarcodeEncoding.MSI to ExpectBarcodeType to ensure that IronBarcode expects MSI type barcodes specifically 
  ExpectBarcodeTypes = BarcodeEncoding.MSI,
};

// Read barcode with additional barcodeReaderOptions from above
var barcodeResults = BarcodeReader.Read("msi.png", barcodeReaderOptions);

// Using a for loop and print out the result 
foreach (BarcodeResult result in barcodeResults)
{
  Console.WriteLine(result.Text);
    // Output: 12345 
}
' Creating MSI Barcode with the value "12345"
Dim myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.MSI)

' Save barcode as image
myBarcode.SaveAsImage("msi.png")

' Reading MSI
Dim barcodeReaderOptions As New BarcodeReaderOptions With {.ExpectBarcodeTypes = BarcodeEncoding.MSI}

' Read barcode with additional barcodeReaderOptions from above
Dim barcodeResults = BarcodeReader.Read("msi.png", barcodeReaderOptions)

' Using a for loop and print out the result 
For Each result As BarcodeResult In barcodeResults
  Console.WriteLine(result.Text)
	' Output: 12345 
Next result
$vbLabelText   $csharpLabel

In the example, we first instantiate a new BarcodeReaderOptions variable and then assign ExpectedBarcodeTypes with the BarcodeEncoding.MSI enum, instructing IronBarcode to expect the MSI barcode instead. Afterwards, we print out the value of the barcode, which would be 12345, in a for loop as the barcodeResults returns an array of BarcodeResults and loop through each result for the text value.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

Beyond development, Curtis has a strong interest in the Internet of Things (IoT), exploring innovative ways to integrate hardware and software. In his free time, he enjoys gaming and building Discord bots, combining his love for technology with creativity.