How to read/write barcodes from/to images and PDF files in VB.NET, C#,ASP.NET using VintaSoftBarcode.NET SDK.

For a copy of the sample project please
click here
to download and install the product evaluation.
Introduction
This simple example demonstrates how to read/write barcodes from/to images or PDF files in your VB.NET, C# or ASP.NET application.
Detail
VintaSoftBarcode.NET SDK has 3 kinds of examples:
- WinForms application written in VB.NET and C#. It allows to open an image or PDF file from disk, choose desired 1D or 2D symbology and recognize barcodes. There is also full featured 1D&2D barcode writer functionality available.
- Web application written on VB.NET/ASP.NET - allows to upload an image file with barcodes to the server and read barcodes from uploaded image.
- Web application written on VB.NET/ASP.NET - allows to create image with 1D, postal or 2D barcode on the fly.
Setup
Download and install the free evaluation version of VintaSoftBarcode.NET SDK
from this link: http://www.vintasoft.com/zip/VSBarcodeNet44.zip.
Running the solution
Open the sample project in Microsoft Visual Studio .NET 2005 or 2008 and build it. You will see an empty interface screen.
Now you can select an image file with barcode by clicking "Load image" button. Select the types of barcodes and necessary recognition settings in "Reader settings". Select the types of barcodes and necessary writer settings in "Writer".
Finally, you should press the "Read barcodes" button and get results in "Reader results".
How the code works:
// Read barcodes from image
static void ReadBarcodesFromImage(Image imageWithBarcodes)
{
BarcodeReader reader = new BarcodeReader();
// read Code39 or Code128 barcodes
reader.Settings.ScanBarcodeTypes = BarcodeType.Code39 | BarcodeType.Code128;
reader.Settings.ScanInterval = 5;
IBarcodeInfo[] results = reader.ReadBarcodes(imageWithBarcodes);
if (results.Length == 0)
Console.WriteLine("No barcodes found.");
else
for (int i = 0; i < results.Length; i++)
{
Console.WriteLine(string.Format("[{0}] {1}", results[i].BarcodeType, results[i].Value));
}
}
// Read barcodes from image file
static void ReadBarcodesFromImageFile(string fileName)
{
ReadBarcodesFromImage(Image.FromFile(fileName));
}
// Read barcodes from PDF file
static void ReadBarcodesFromPDFFile(string fileName)
{
// create pdf image viewer
PdfImageViewer pdfImageViewer = new PdfImageViewer(fileName);
for (int i = 0; i < pdfImageViewer.PageCount; i++)
{
// get image names at page i
string[] names = pdfImageViewer.GetImageNames(i);
for (int j = 0; j < names.Length; j++)
{
Image imageFromPDF;
try
{
// get image from PDF file
imageFromPDF = pdfImageViewer.GetImage(i, names[j]);
}
catch (Exception e)
{
Console.WriteLine(string.Format("Error at page {0}: {1}", i, e));
continue;
}
Console.WriteLine("Read barcodes from page {0}, image {1}:", i, names[j]);
// read barcodes
ReadBarcodesFromImage(imageFromPDF);
// free resources
imageFromPDF.Dispose();
}
}
}
// Generate Code128 barcode
static Image GenerateBarcode(string message)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Settings.Barcode = BarcodeType.Code128;
writer.Settings.Value = message;
// write barcode
Image barcodeImage = writer.WriteBarcode();
// read this barcode
ReadBarcodesFromImage(barcodeImage);
return barcodeImage;
}
[VB.NET]
' Read barcodes from image
Private Shared Sub ReadBarcodesFromImage(ByVal imageWithBarcodes As Image)
Dim reader As New BarcodeReader()
' read Code39 or Code128 barcodes
reader.Settings.ScanBarcodeTypes = BarcodeType.Code39 Or BarcodeType.Code128
reader.Settings.ScanInterval = 5
Dim results As IBarcodeInfo() = reader.ReadBarcodes(imageWithBarcodes)
If results.Length = 0 Then
Console.WriteLine("No barcodes found.")
Else
For i As Integer = 0 To results.Length - 1
Console.WriteLine(String.Format("[{0}] {1}", results(i).BarcodeType, results(i).Value))
Next
End If
End Sub
' Read barcodes from image file
Private Shared Sub ReadBarcodesFromImageFile(ByVal fileName As String)
ReadBarcodesFromImage(Image.FromFile(fileName))
End Sub
' Read barcodes from PDF file
Private Shared Sub ReadBarcodesFromPDFFile(ByVal fileName As String)
' create pdf image viewer
Dim pdfImageViewer As New PdfImageViewer(fileName)
For i As Integer = 0 To pdfImageViewer.PageCount - 1
' get image names at page i
Dim names As String() = pdfImageViewer.GetImageNames(i)
For j As Integer = 0 To names.Length - 1
Dim imageFromPDF As Image
Try
' get image from PDF file
imageFromPDF = pdfImageViewer.GetImage(i, names(j))
Catch e As Exception
Console.WriteLine(String.Format("Error at page {0}: {1}", i, e))
Continue For
End Try
Console.WriteLine("Read barcodes from page {0}, image {1}:", i, names(j))
' read barcodes
ReadBarcodesFromImage(imageFromPDF)
' free resources
imageFromPDF.Dispose()
Next
Next
End Sub
' Generate Code128 barcode
Private Shared Function GenerateBarcode(ByVal message As String) As Image
Dim writer As New BarcodeWriter()
writer.Settings.Barcode = BarcodeType.Code128
writer.Settings.Value = message
' write barcode
Dim barcodeImage As Image = writer.WriteBarcode()
' read this barcode
ReadBarcodesFromImage(barcodeImage)
Return barcodeImage
End Function
Conclusion
This example demonstrates how to read barcodes from image, image file or PDF document and generate Code 128 barcode in VB.NET, C# or ASP.NET application.
This simple example only shows just a small sample of the full capabilities of VintaSoftBarcode.NET SDK.
Visit
VintaSoft Ltd
for more information and more samples.