原創|其它|編輯:郝浩|2012-11-07 17:43:27.000|閱讀 1128 次
概述:在本文中,我們將使用ASP.NET web services創建條形碼。我們還將創建含有條形碼的Windows Forms和Console應用程序。該過程會用到Aspose.BarCode這個控件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在本文中,我們將使用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(); }
[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方法需要客戶端以下兩個參數:
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,得到條形碼,并保存在本地磁盤上。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網