How to Use aspose. barcode to create a barcode

Source: Internet
Author: User

Source from: huide Control Network http://www.evget.com/zh-CN/Info/catalog/18049.html

In this article, we will use ASP. NET web services to create a bar code. We will also create Windows Forms and console applications with bar codes. The aspose. barcode control is used in this process.

What are the benefits of doing so?

The main advantage of web services is that the software is integrated with external applications. The standardized request/response model can benefit any client application based on XML Web Service. The following is a brief representation of the bar code service. The client does not need to install aspose. barcode for. Net here. They only need to send two string values (Code text and symbols) and will get the barcode (byte array) from the server ).

Open Microsoft Visual Studio and create a new project named "ASP. NET web service application ". Add the following reference.

1. system. Drawing from. Net tab in the "add reference" dialog box

2. aspose. barcode.

Find the location where aspose. barcode for. NET is installed and select. Visual Studio adds a default class "service1" to the Web Service Project of service1.asmx. Open it and add the following methods for 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 

The web method requires the following two parameters on the client:

1. codetext

2. Symbology

These parameters are string type. These parameters are passed to the barcodebuilder class, and then a bar code is created and sent to the client as a byte array.

Use Web Service in Windows Forms applications

Open Visual Studio and create a new type of "Windows application" project. Name the project "getbarcodewinforms ". Right-click "references", select, and select "add service reference" from the menu to add a reference to the web service. Enter the address of the web service. After obtaining the correct results, enter "barcodeservice" in the namespace naming field and click "OK" to add reference.

Shows the design form:

It contains the following controls:

1. textbox: Enter the code

2. ComboBox: Input symbol type

3. Button: Call Web Service

4. picturebox: Display bar code

Click the event button for the code to add the following code.

[C #]

// Initialize the Barcode Web ServiceBarCodeService.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 arraybyte[] arrBarcodeImage = barcodeService.GetBarcode(txtCodetext.Text, cmbSymbology.Text);// Create an instance of Image from the byte arrayMemoryStream imgStream = new MemoryStream(arrBarcodeImage);Image imgBarcode = Bitmap.FromStream(imgStream);// Assign the barcode image to the Picturebox controlpicBarcodeImage.Image = imgBarcode;picBarcodeImage.Height = imgBarcode.Height;picBarcodeImage.Width = imgBarcode.Width; 

[VB. NET]

' Initialize the Barcode Web ServiceDim 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 arrayDim arrBarcodeImage As Byte() = barcodeService.GetBarcode(txtCodetext.Text, cmbSymbology.Text)' Create an instance of Image from the byte arrayDim imgStream As MemoryStream = New MemoryStream(arrBarcodeImage)Dim imgBarcode As Image = Bitmap.FromStream(imgStream)' Assign the barcode image to the Picturebox controlpicBarcodeImage.Image = imgBarcodepicBarcodeImage.Height = imgBarcode.HeightpicBarcodeImage.Width = imgBarcode.Width

Run the application, specify some values, and click "Get barcode. The application uses the barcode web service and obtains the barcode from it. The bar code is displayed in the following form.

Use Web Service from the console application Console

Create a new "console application" project in Visual Studio and name the project "getbarcodeconsole ". Add the reference to the bar code service in the same way as in the winforms application. Write the following 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 TryConsole.WriteLine("Press any key to exit....")Console.ReadKey() 

Run the application. It uses the barcode web service to obtain the barcode and save it on a local disk.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.