Home > Code > C# > Create Barcodes using ASP.NET Web Service, Windows Forms & Console Apps

Create Barcodes using ASP.NET Web Service, Windows Forms & Console Apps

by sher azam   on Oct 29, 2013   Category: C#   |  Views: 2409    |  Points: 25   |  Gold 


This Technical tip explains how to use ASP.NET web services to create barcodes. We will also create Windows Forms and Console application to request barcode image. Web services provide a major benefit in integrating the software with external applications. With standardized request/response model, any client application that can utilize XML web service can benefit from it. Aspose.BarCode for .NET can easily be used in ASP.NET web services to generate the barcodes and serve the barcode image to various clients.

Barcode Web Service

Open Microsoft Visual Studio and create a new project of type “ASP.NET Web Service Application”. Name the project as “BarCodeService”. Add reference to the following .NET assemblies.

• System.Drawing from .NET tab of “Add Reference” dialog box
• Aspose.BarCode.

Browse to the location where Aspose.BarCode for .NET is installed and select. Visual Studio adds a default class “Service1” to the Web Service project in Service1.asmx file. Open it and add the following method to this class.

[C#]

[WebMethod]
public byte[] GetBarcode(string strCodetext, string strSymbology)
{
// Initialize BarCodeBuilder
BarCodeBuilder builder = new BarCodeBuilder();
// Set codetext
builder.CodeText = strCodetext;
// Set barcode symbology
builder.SymbologyType = (Symbology) Enum.Parse(typeof(Symbology), strSymbology, true);

// Create and save the barcode image to memory stream
MemoryStream imgStream = new MemoryStream();
builder.Save(imgStream, ImageFormat.Png);

// Return the barcode image as a byte array
return imgStream.ToArray();
}

[VB.NET]

<WebMethod> _
Public Function GetBarcode(ByVal strCodetext As String, ByVal strSymbology As String) As Byte()
' Initialize BarCodeBuilder
Dim builder As BarCodeBuilder = New BarCodeBuilder()
' Set codetext
builder.CodeText = strCodetext
' Set barcode symbology
builder.SymbologyType = CType(System.Enum.Parse(GetType(Symbology), strSymbology, True), Symbology)

' Create and save the barcode image to memory stream
Dim imgStream As MemoryStream = New MemoryStream()
builder.Save(imgStream, ImageFormat.Png)

' Return the barcode image as a byte array
Return imgStream.ToArray()
End Function

Consume Web Service from Windows Forms Application

Open Visual Studio and create a new project of type “Windows Application”. Name the project as “ GetBarCodeWinForms ”. Add reference to the web service by right clicking on “References” and then choosing “Add Service Reference” from the context menu. Type the address of web service or discover it. After getting the correct service, give “BarCodeService” in the Namespace and click on “Ok” button to add the reference.

[C#]

// Initialize the Barcode Web Service
BarCodeService.Service1SoapClient barcodeService = new BarCodeService.Service1SoapClient();
// Call the GetBarcode web method
// Pass codetext and symbology in parameters
// Get the barcode image returned from the web method in the form of byte array
byte[] arrBarcodeImage = barcodeService.GetBarcode(txtCodetext.Text, cmbSymbology.Text);

// Create an instance of Image from the byte array
MemoryStream imgStream = new MemoryStream(arrBarcodeImage);
Image imgBarcode = Bitmap.FromStream(imgStream);

// Assign the barcode image to the Picturebox control
picBarcodeImage.Image = imgBarcode;
picBarcodeImage.Height = imgBarcode.Height;
picBarcodeImage.Width = imgBarcode.Width;

[VB.NET]

' Initialize the Barcode Web Service
Dim barcodeService As BarCodeService.Service1SoapClient = New BarCodeService.Service1SoapClient()
' Call the GetBarcode web method
' Pass codetext and symbology in parameters
' Get the barcode image returned from the web method in the form of byte array
Dim arrBarcodeImage As Byte() = barcodeService.GetBarcode(txtCodetext.Text, cmbSymbology.Text)

' Create an instance of Image from the byte array
Dim imgStream As MemoryStream = New MemoryStream(arrBarcodeImage)
Dim imgBarcode As Image = Bitmap.FromStream(imgStream)

' Assign the barcode image to the Picturebox control
picBarcodeImage.Image = imgBarcode
picBarcodeImage.Height = imgBarcode.Height
picBarcodeImage.Width = imgBarcode.Width

Consume Web Service from a Console Application

Create a new project in Visual Studio and choose “Console Application” in project type. Name the project as “ GetBarCodeConsole ”. Add the reference to the barcode service, just as we gave above in the winforms application. Write the below code in the main() method.

[C#]

try
{
// Initialize the Barcode Web Service
BarCodeService.Service1SoapClient c = new GetBarCodeConsole.BarCodeService.Service1SoapClient();
// Call the GetBarcode web method
// Pass codetext and symbology in parameters
// Get the barcode image returned from the web method in the form of byte array
byte[] arrBarcodeImage = c.GetBarcode("console application", "pdf417");

// Save the byte array (barcode image) to disk
FileStream imgWriter = new FileStream("barcode.png", FileMode.Create);
imgWriter.Write(arrBarcodeImage, 0, arrBarcodeImage.Length);
imgWriter.Close();

// Open the barcode image
Process.Start("barcode.png");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Console.WriteLine("Press any key to exit....");
Console.ReadKey();

[VB.NET]

Try
' Initialize the Barcode Web Service
Dim c As BarCodeService.Service1SoapClient = New GetBarCodeConsole.BarCodeService.Service1SoapClient()
' Call the GetBarcode web method
' Pass codetext and symbology in parameters
' Get the barcode image returned from the web method in the form of byte array
Dim arrBarcodeImage As Byte() = c.GetBarcode("console application", "pdf417")

' Save the byte array (barcode image) to disk
Dim imgWriter As FileStream = New FileStream("barcode.png", FileMode.Create)
imgWriter.Write(arrBarcodeImage, 0, arrBarcodeImage.Length)
imgWriter.Close()

' Open the barcode image
Process.Start("barcode.png")
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

Console.WriteLine("Press any key to exit....")
Console.ReadKey()

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.

More about Aspose.BarCode for .NET

- 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


  Re :Create Barcodes using ASP.NET Web Service, Windows Forms & Console Apps   
Posted by Leo_21
on 03/06/2014
Points : 5

hi,

thx for the great information. but still I'm want to recommend a 100% free Spire.Barcode API ,which satisfied nearly all requirements with creating and reading barcode in .NET platform.

refer-http://freebarcode.codeplex.com/

thanks and regards

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts