News Category

Programme Guide for Spire.Barcode

Spire.BarCode for .NET is a professional barcode component specially designed for .NET developers (C#, VB.NET, ASP.NET) to generate, read 1D & 2D barcodes. Developers and programmers can use Spire.BarCode to add Enterprise-Level barcode formats to their .net applications (ASP.NET, WinForms) quickly and easily. Below is how to use Spire.BarCode for .NET.

Step 1: Create Project

Create a C#/VB.NET Windows Forms Application project in visual studio, name it Barcode.

Step 2: Add Spire.Barcode.dll

  • In Toolbox, right-click the blank area, select "Add Tab", name it "Spire.Barcode Controls".
  • Right-click "Spire.Barcode Controls", select "Choose Items", select ".NET Framework Components", Click "Browse", find the Spire.Barcode.dll and double-click it.
  • Then you will see "BarCodeControl" shown in "Spire.Barcode Controls" in Toolbox.

Step 3: Add Controls

Here is what the window looks like:

Step 4: Generate Barcode Image

btnCreate_Click is the method to generate barcode image. There are two import classes-BarCodeControl and BarCodeGenerator in this method. BarCodeControl stores information about barcode.

Here are some introductions about some of its properties:

Name of Property Description
Data Stores the data that is to be encoded to one-dimension barcode.
Data2D Stores the data that is to be encoded to two-dimension barcode.
Type Indicates the type of barcode that is to be generated.
HasBorder Indicates whether barcode image has border.
BorderDashStyle Stores the type of border barcode image has.
BarHeight Stores the height of barcode image.
CheckB_BarcodeText Indicates whether to show the barcode text.
TextFont Stores the font of barcode text.
ForeColor Stores the fore color of barcode image.
CheckB_Sum Indicates whether to show the checksum digit in Code128 and EAN128 Barcodes.

 

BarCodeGenerator is the class to generate barcode image. Its constructor takes one parameter – a BarCodeControl instance. It has a method called GenerateImage() whose return value is Image object to generate image.

[C#]
//Generate the barcode based on the this.barCodeControl1
BarCodeGenerator generator = new BarCodeGenerator(this.barCodeControl1);
Image barcode = generator.GenerateImage();
            
//save the barcode as an image
barcode.Save("barcode.png");
[VB.NET]
'Generate the barcode based on the barCodeControl1
Dim generator As New BarCodeGenerator(barCodeControl1)
Dim barcode As Image = generator.GenerateImage()

'save the barcode as an image
barcode.Save("barcode.png")

Step 5: Scan Barcode Image

BarcodeScanner is the class to scan barcode image. Call its method Scan with the Bitmap object containing the barcode image, it returns a string [] value where the scanning result is stored.

And btnScan_Click uses the class BarcodeScanner to scan barcode image in its code.

Here is the code in btnScan_Click.

[C#]
private void btnScan_Click(object sender, EventArgs e)
{
//scan the barcode
string[] datas = Spire.Barcode.BarcodeScanner.Scan("barcode.png");

//show the scan result
this.TextB_ScanResult.Text = datas[0];
}
[VB.NET]
Private Sub btnScan_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnScan.Click
'scan the barcode
Dim datas() As String = Spire.Barcode.BarcodeScanner.Scan("barcode.png")

'show the scan result
Me.TextB_ScanResult.Text = datas(0)
End Sub

Step 6. Running

Run the project. You will see a window opened.

Put in some settings and click the button "Create". You will see the generated barcode image.

Click the button "Scan". You will see the barcode text shown in TextB_ScanResult.

When generating a QR code, you might want to add a custom image such as your company’s logo or your personal profile image to it. In this article, you will learn how to achieve this task programmatically in C# and VB.NET using Spire.Barcode for .NET library.

Install Spire.Barcode for .NET

To begin with, you need to add the DLL files included in the Spire.Barcode for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Barcode

Note: This feature relies on a commercial license. If you want to test it, please go to the end of this article to request a temporary license.

Generate QR Code with a Logo Image in C# and VB.NET

The following are the steps to generate a QR code with a logo image:

  • Create a BarcodeSettings object.
  • Set barcode type, error correction level and data etc. using BarcodeSettings.Type, BarcodeSettings.QRCodeECL and BarcodeSetting.Data properties.
  • Set logo image using BarcodeSettings.QRCodeLogoImage property.
  • Create a BarCodeGenerator object based on the settings.
  • Generate QR code image using BarCodeGenerator.GenerateImage() method.
  • Save the image using Image.Save() method.
  • C#
  • VB.NET
using Spire.Barcode;
using Spire.License;
using System.Drawing;

namespace AddLogoToQR
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load license
            Spire.License.LicenseProvider.SetLicenseFileFullPath("license.elic.xml");

            //Create a BarcodeSettings object
            BarcodeSettings settings = new BarcodeSettings();

            //Set barcode type, error correction level, data, etc.
            settings.Type = BarCodeType.QRCode;
            settings.QRCodeECL = QRCodeECL.M;
            settings.ShowText = false;
            settings.X = 2.5f;
            string data = "www.e-iceblue.com";
            settings.Data = data;
            settings.Data2D = data;

            //Set logo image
            settings.QRCodeLogoImage = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png");

            //Generate QR image based on the settings
            BarCodeGenerator generator = new BarCodeGenerator(settings);
            Image image = generator.GenerateImage();
            image.Save("QR.png", System.Drawing.Imaging.ImageFormat.Png);
        }
    }
}

C#/VB.NET: Generate QR Code with a Logo Image

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

EAN-13, based upon the UPC-A standard, is used world-wide for marking retail goods. The 13-digit EAN-13 number consists of four components:

  • Country code - 2 or 3 digits
  • Manufacturer Code - 5 to 7 digits
  • Product Code - 3 to 5 digits
  • Check digit - last digit

The following code snippets demonstrate how to create EAN-13 barcode image using Spire.Barcode in C#.

Step 1: Create a BarcodeSettings instance.

BarcodeSettings settings = new BarcodeSettings();

Step 2: Set the barcode type as EAN13.

settings.Type = BarCodeType.EAN13;

Step 3: Set the data to encode.

settings.Data = "123456789012";

Step 4: Calculate checksum and add the check digit to barcode.

settings.UseChecksum = CheckSumMode.ForceEnable;

Step 5: Display barcode's text on bottom and centrally align the text.

settings.ShowTextOnBottom = true;
settings.TextAlignment = StringAlignment.Center;

Step 6: Generate barcode image based on the settings and save it in .png format.

BarCodeGenerator generator = new BarCodeGenerator(settings);
Image image = generator.GenerateImage();
image.Save("EAN-13.png", System.Drawing.Imaging.ImageFormat.Png);

Output:

How to Create EAN-13 Barcode in C#

Full Code:

using Spire.Barcode;
using System.Drawing;

namespace EAN-13
{
    class Program
    {
        static void Main(string[] args)
        {
            BarcodeSettings settings = new BarcodeSettings();
            settings.Type = BarCodeType.EAN13;
            settings.Data = "123456789012";
            settings.UseChecksum = CheckSumMode.ForceEnable;
            settings.ShowTextOnBottom = true;
            settings.TextAlignment = StringAlignment.Center;
            BarCodeGenerator generator = new BarCodeGenerator(settings);
            Image image = generator.GenerateImage();
            image.Save("EAN-13.png", System.Drawing.Imaging.ImageFormat.Png);
        }
    }
}

A DataMatrix code is a two-dimensional barcode consisting of black and white "cells" or modules arranged in either a square or rectangular pattern. The information to be encoded can be text or numeric data.

Following code snippets show how to create a DataMatrix barcode image using Spire.Barcode.

Step 1: Create a BarcodeSettings instance.

BarcodeSettings settings = new BarcodeSettings();

Step 2: Set the width of barcode module

settings.X = 2;

Step 3: Set the barcode type as DataMatrix.

settings.Type = BarCodeType.DataMatrix;

Step 4: Set the data and display text

settings.Data = "ABC 123456789ABC 123456789ABC 123456789";
settings.Data2D = "ABC 123456789ABC 123456789ABC 123456789";

Step 5: Set the DataMatrix symbol shape to square.

settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Square;

Step 6: Generate barcode image based on the settings and save it in .png format.

BarCodeGenerator generator = new BarCodeGenerator(settings);
Image image = generator.GenerateImage();
image.Save("DataMatrix.png", System.Drawing.Imaging.ImageFormat.Png);

How to Create DataMatrix Barcode in C#

To create a rectangular DataMatrix barcode, set the DataMatrixSymbolShape property to Rectangle.

settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Rectangle;

How to Create DataMatrix Barcode in C#

Full Code:

using Spire.Barcode;
using System.Drawing;

namespace DataMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            BarcodeSettings settings = new BarcodeSettings();
            settings.Type = BarCodeType.DataMatrix;
            settings.X = 1.5f;
            settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Square;
            //rectangular DataMatrix barcode
            //settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Rectangle;  
            settings.Data = "ABC 123456789ABC 123456789ABC 123456789";
            settings.Data2D = "ABC 123456789ABC 123456789ABC 123456789";
            BarCodeGenerator generator = new BarCodeGenerator(settings);
            Image image = generator.GenerateImage();
            image.Save("DataMatrix.png", System.Drawing.Imaging.ImageFormat.Png);
        }
    }
}

How to Scan Barcode in C#

2017-06-28 08:40:26 Written by support iceblue

We have already demonstrated how to generate 1D barcode and 2D barcodes by using Spire.Barcode. This article will demonstrate how to scan the barcode images with Spire.Barcode. Spire.Barcode supports scanning the barcode image in .bmp, .png and .jpg image format. We will use Spire.Barcode to scan both 1D barcode and 2D barcode for example.

Scan Code39 barcode image:

string[] data = Spire.Barcode.BarcodeScanner.Scan("Code39.png",
Spire.Barcode.BarCodeType.Code39);

How to Scan Barcode in C#

Scan QRCode barcode image:

string[] data = Spire.Barcode.BarcodeScanner.Scan("QRCode.png", Spire.Barcode.BarCodeType.QRCode);

How to Scan Barcode in C#