原創(chuàng)|其它|編輯:郝浩|2012-11-07 17:43:27.000|閱讀 1128 次
概述:在本文中,我們將使用ASP.NET web services創(chuàng)建條形碼。我們還將創(chuàng)建含有條形碼的Windows Forms和Console應(yīng)用程序。該過程會(huì)用到Aspose.BarCode這個(gè)控件。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在本文中,我們將使用ASP.NET web services創(chuàng)建條形碼。我們還將創(chuàng)建含有條形碼的Windows Forms和Console應(yīng)用程序。該過程會(huì)用到Aspose.BarCode這個(gè)控件。
這樣做有什么好處呢?
Web services的主要優(yōu)勢(shì)在于軟件與外部應(yīng)用程序集成。標(biāo)準(zhǔn)化的請(qǐng)求/響應(yīng)模型,任何基于XML web service的客戶端應(yīng)用程序都可以從中受益。以下是簡(jiǎn)短的條形碼服務(wù)的表現(xiàn)形式。客戶端不需要在此安裝Aspose.BarCode for .NET。他們只需發(fā)送兩個(gè)字符串值(代碼電文和符號(hào)),就將從服務(wù)端獲取條形碼(字節(jié)數(shù)組)。
打開Microsoft Visual Studio,并創(chuàng)建一個(gè)“ASP.NET Web Service Application”新項(xiàng)目,命名為“BarCodeService”。 添加以下引用。
1.“Add Reference”對(duì)話框的System.Drawing from .NET選項(xiàng)卡
2. Aspose.BarCode。
找到 Aspose.BarCode for .NET安裝的位置并選擇。Visual Studio會(huì)添加了一個(gè)默認(rèn)的類“Service1“到Service1.asmx文檔的Web Service項(xiàng)目。 打開它,并為這個(gè)類添加以下方法。
[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
web方法需要客戶端以下兩個(gè)參數(shù):
1.Codetext
2.Symbology
這些參數(shù)為String字符串類型。這些參數(shù)被傳遞到BarCodeBuilder類,然后創(chuàng)建條形碼,并以字節(jié)數(shù)組的形式給客戶端發(fā)送條形碼。
使用Windows Forms應(yīng)用中的Web Service
打開Visual Studio,并創(chuàng)建一個(gè)新類型“Windows Application”的項(xiàng)目。命名項(xiàng)目為“GetBarCodeWinForms”。通過右鍵單擊“References”,選擇,然后從菜單中選擇““Add Service Reference”為web service添加引用。鍵入web service的地址。在得到正確的結(jié)果之后,在Namespace命名域中輸入“BarCodeService”,點(diǎn)擊“Ok”按鈕以添加引用。
設(shè)計(jì)形式如下圖所示:
它包含以下控件:
1.Textbox:輸入代碼
2.Combobox:輸入符號(hào)類型
3.Button:調(diào)用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
運(yùn)行該應(yīng)用程序,指定某些值,點(diǎn)擊“Get Barcode”按鈕。應(yīng)用程序?qū)⑹褂脳l形碼web service,并從中獲取條形碼。條形碼將顯示在如下窗體中。
從Console Application控制臺(tái)應(yīng)用程序使用Web Service
在Visual Studio中創(chuàng)建一個(gè)“Console Application”新項(xiàng)目,將項(xiàng)目命名為“GetBarCodeConsole”。 將該引用添加到條碼服務(wù)中,方法和winforms應(yīng)用程序中的相同。在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()
運(yùn)行該應(yīng)用程序,它將使用條形碼web service,得到條形碼,并保存在本地磁盤上。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件網(wǎng)