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.
//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");
'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.
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]; }
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.