Home > Code > C# > How to Read & Mark Barcode Regions in an Image inside .NET Applications

How to Read & Mark Barcode Regions in an Image inside .NET Applications

by sher azam   on Aug 19, 2015   Category: C#   |  Views: 595    |  Points: 25   |  Gold 


This Technical tip explains how to mark barcode regions in the Image inside .NET applications. In this section, we will read the image and mark all the barcode regions, for all the recognized barcodes in the image. The barcode region is the part of the image that only contains the barcode itself. In a large image, it is possible that there are other texts or images along with the barcode. Getting the barcode region will separate the barcodes from other text/objects in the image by marking their edges or by filling with some color. First, we will read the BarCodes in the image using the BarCodeReader.Read() method. Then, we will get the region of the barcode using BarCodeReader.GetRegion() method, which will return an instance of type BarCodeRegion. We can then draw edges around the barcode using BarCodeRegion.DrawBarCodeEdges() method.

//[C# Code Sample]

// create an instance of BarCodeReader class and specify the image and symbology
BarCodeReader reader;
reader = new BarCodeReader(@"code39Extended.jpg", BarCodeReadType.Code39Extended);
int counter = 0;
// read all the barcodes from the images
while (reader.Read())
{
// display the symbology type
Console.WriteLine("BarCode Type: " + reader.GetReadType());
// display the codetext
Console.WriteLine("BarCode CodeText: " + reader.GetCodeText());
// get the barcode region
Aspose.BarCodeRecognition.BarCodeRegion region = reader.GetRegion();
if (region != null)
{
// Initialize an object of type Image to get the Graphics object
Image img = System.Drawing.Image.FromFile(@"code39Extended.jpg");
// initialize graphics object from the image
Graphics g = Graphics.FromImage(img);
// draw the barcode edges
region.DrawBarCodeEdges(g, new Pen(Color.Red, 1f));
// save the image
img.Save(string.Format(@"edge_{0}.png", counter++));
// fill the barcode area with some color
region.FillBarCodeRegion(g, Brushes.Green);
img.Save(string.Format(@"fill_{0}.png", counter++));
}
}
reader.Close();

//[VB.NET Code Sample]

'create an instance of BarCodeReader class and specify the image and symbology
Dim reader As BarCodeReader
reader = New BarCodeReader("c:\test.jpg", BarCodeReadType.Code39Standard)
Dim counter As Integer
'read all the barcodes from the images
While reader.Read()
counter = counter + 1
'display the symbology type
Console.WriteLine("BarCode Type: " + reader.GetReadType().ToString)
'display the code text
Console.WriteLine("BarCode CodeText: " + reader.GetCodeText())
Dim region As BarCodeRegion
'get the barcode region
region = reader.GetRegion()
If Not region Is Nothing Then
'Initialize an object of type Image to get the Graphics object
Dim img As Image = System.Drawing.Image.FromFile("c:\test.jpg")
'initialize graphics object from the image
Dim g As Graphics = Graphics.FromImage(img)
'draw the barcode edges
region.DrawBarCodeEdges(g, New Pen(Color.Red, 1.0F))
img.Save(String.Format(".\edge_{0}.png", counter))
'fill the barcode area with some color
region.FillBarCodeRegion(g, Brushes.Green)
img.Save(String.Format(".\fill_{0}.png", counter))
End If
End While
reader.Close()



Overview: Aspose.BarCode for .NET

Aspose.BarCode is a .NET component for generation and recognition of Linear and 2D barcodes on all kinds of .NET applications. It supports WPF with 29+ Barcode symbologies like OneCode, QR, Aztec, MSI, EAN128, EAN14, SSCC18, Code128, Code39, Postnet, MarcoPDF417, Datamatrix, UPCA etc. Other features include barcode insertion in PDF, Word and Excel documents. Also take image output in BMP, GIF, JPEG, PNG and WMF formats. You can also control image styles such as background color, bar color etc.

- Homepage of Aspose.BarCode for .NET: http://www.aspose.com/.net/barcode-component.aspx

- Download of Aspose.BarCode for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.barcode-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts