您的位置:首页 > 编程语言 > ASP

如何使用Aspose.BarCode创建条形码

2012-11-08 09:47 495 查看
原文来自:慧都控件网http://www.evget.com/zh-CN/Info/catalog/18049.html

在本文中,我们将使用ASP.NET web services创建条形码。我们还将创建含有条形码的Windows Forms和Console应用程序。该过程会用到Aspose.BarCode这个控件。

这样做有什么好处呢?

Web services的主要优势在于软件与外部应用程序集成。标准化的请求/响应模型,任何基于XML web service的客户端应用程序都可以从中受益。以下是简短的条形码服务的表现形式。客户端不需要在此安装Aspose.BarCode for .NET。他们只需发送两个字符串值(代码电文和符号),就将从服务端获取条形码(字节数组)。



打开Microsoft Visual Studio,并创建一个“ASP.NET Web Service Application”新项目,命名为“BarCodeService”。 添加以下引用。

1.“Add Reference”对话框的System.Drawing from .NET选项卡

2. Aspose.BarCode。

找到 Aspose.BarCode for .NET安装的位置并选择。Visual Studio会添加了一个默认的类“Service1“到Service1.asmx文档的Web Service项目。 打开它,并为这个类添加以下方法。

[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();
}

[b][VB.NET][/b]
<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

web方法需要客户端以下两个参数:

1.Codetext

2.Symbology

这些参数为String字符串类型。这些参数被传递到BarCodeBuilder类,然后创建条形码,并以字节数组的形式给客户端发送条形码。

使用Windows Forms应用中的Web Service

打开Visual Studio,并创建一个新类型“Windows Application”的项目。命名项目为“GetBarCodeWinForms”。通过右键单击“References”,选择,然后从菜单中选择““Add Service Reference”为web service添加引用。键入web service的地址。在得到正确的结果之后,在Namespace命名域中输入“BarCodeService”,点击“Ok”按钮以添加引用。



设计形式如下图所示:



它包含以下控件:

1.Textbox:输入代码

2.Combobox:输入符号类型

3.Button:调用web service

4.Picturebox:显示条形码

为代码的按钮单击事件添加以下代码。

[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

运行该应用程序,指定某些值,点击“Get Barcode”按钮。应用程序将使用条形码web service,并从中获取条形码。条形码将显示在如下窗体中。



从Console Application控制台应用程序使用Web Service

在Visual Studio中创建一个“Console Application”新项目,将项目命名为“GetBarCodeConsole”。 将该引用添加到条码服务中,方法和winforms应用程序中的相同。在main()方法中编写以下代码。

[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()

运行该应用程序,它将使用条形码web service,得到条形码,并保存在本地磁盘上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐