`
csstome
  • 浏览: 1519640 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

BarCode WbeService...

阅读更多

BarCode WbeService

The Article is come form codeproject website

Web address - http://www.codeproject.com/KB/cpp/wsbarcode.aspx

Introduction

Some days ago,I was asked to find a solution to create a barcode to be printed on documents produced by a web application. After trying to find some components that produced barcodes,I soon realized that their price was very high when dealing with an unlimited number of clients licences. I needed an alphanumeric barcode representation and the preferred barcode representation was Code39.

In order to expose this service to the maximum of clients while delivering a standardized solution,I thought of writing a web service that would generate the barcode dynamically and return it streamlined as an image.

This article describes the solution that I’ve implemented.

The Barcode Generation

Instead of writing a code39 barcode generator that mimics the algorithm for that barcode representation, my idea was to use one of the freely available barcode fonts to produce the barcode (http://www.squaregear.net/fonts/free3of9.shtml).

So my approach was simple:

  1. Load the Barcode Font

  2. Create an image object

  3. Draw a string into that image using a code39 barcode font

  4. Return that image serialized.

Using the Code39 Font...

The way to use a font in windows is simple, all you have to do is install it (by copying it to the c:\WINDOWS\Fonts - under XP) and just use it.

Unfortunately, the ASP.NET graphic context does not allow you to use any font (free3of9.ttf for example) because .NET GDI only uses/enumerates OpenType fonts. So what you have to do is create a temporary font object.

This method is very straighforward, as you can see in the code sample below:

// Create a private font collection

objectPrivateFontCollection pfc=new PrivateFontCollection();

// Load in the temporary barcode font

pfc.AddFontFile("c:\\barcodefont\\free3of9.ttf");

// Select the font family to use

FontFamily family=new FontFamily("Free 3 of 9",pfc);

// Create the font object with size 30

Font c39Font=new Font(family,30);

With this easy way, you get a font object mapped to the barcode font so that you can create the barcode.

Creating the Barcode Image Container

The image creation is very simple. .NET classes allow you to generate images on the fly. So, in order to create a image large enough to accommodate the barcode, first you need to determine the size that will be occupied by the code string drawing, using the barcode font.

You can do it using the MeasureString method:

// Create a temporary bitmap...

Bitmap tmpBitmap = new Bitmap(1,1,PixelFormat.Format32bppArgb);

objGraphics = Graphics.FromImage(tmpBitmap);

// measure the barcode size...

SizeF barCodeSize=objGraphics.MeasureString(barCodeString,c39Font);

The returned type barCodeSize has the width, and the height that will be occupied by the code string drawing.

Draw the Barcode

So now we need to draw the barcode. We will use the code39 barcode font object instantiated earlier.

Assuming the the barcode variable holds the barcode string, the required code is:

// Draw the barcode

objGraphics.DrawString(barCode, Code39Font, new solidBrush(Color.Black),0,0);

Please note that usualy the code39 barcodes are represented concatenating the char (*) at the beginning and end of the barcode string…meaning that code 123456 has to be written as *123456*. But I will leave that to your experience.

Serialize/Deserialize the Image

In order to return the image from the web service method, you now have to serialize the image, meaning that your web method has to return an array of bytes.

This way, you have to create a stream from the bitmap image, and return it as an array of bytes. Once again, the .NET framework makes it easy for us do perform that task:

// Create stream....

MemoryStream ms = new MemoryStream();

// save the image to the stream

objBitmap.Save(ms ,ImageFormat.Png);

//return an array of bytes....

return ms.GetBuffer();

On the other end (the client side) when you are consuming the web service, you need to be able to deserialize the array of bytes back to the image:

Byte[] imgBarcode;

// Call the webservice to create the barcode...

// Create a stream....

MemoryStream memStream = new MemoryStream(imgBarcode);

// Recreate the image from the stream Bitmap bmp=new Bitmap(memStream);

A final note about the sample code attached…

After creating the barcode web app that will be your webservice, you need to configure the web.config file in order to specify where your barcode font is located. Search for the following section and make your changes accordingly.

<appSettings>

<add key="BarCodeFontFile" value="c:\temp\font\FREE3OF9.TTF" />

<add key="BarCodeFontFamily" value="Free 3 of 9" />

</appSettings>

--the below is I changed the program to vb.net code.

-web service class: BarCode39.vb

Imports Microsoft.VisualBasic

Imports System

Imports System.Drawing

Imports System.Drawing.Imaging

Imports System.Drawing.Text

Imports System.IO

Imports System.Text.RegularExpressions

Namespace Barcodes

Public Class BarCode39

Private Const lint_ItemSepHeight = 3

Private lint_TitleSize As SizeF = SizeF.Empty

Private lint_BarCodeSize As SizeF = SizeF.Empty

Private lint_CodeStringSize As SizeF = SizeF.Empty

Private lstg_TitleString As String

Private lfnt_TitleFont As Font

Sub New()

lfnt_TitleFont = New Font("Arial", 10)

lfnt_CodeStringFont = New Font("Arial", 10)

End Sub

Public Property Title() As String

Get

Return lstg_TitleString

End Get

Set(ByVal value As String)

lstg_TitleString = value

End Set

End Property

Public Property TitleFont() As Font

Get

Return lfnt_TitleFont

End Get

Set(ByVal value As Font)

lfnt_TitleFont = value

End Set

End Property

Private lbool_ShowCodeString As Boolean

Private lfnt_CodeStringFont As Font

Public Property ShowCodeString() As Boolean

Get

Return lbool_ShowCodeString

End Get

Set(ByVal value As Boolean)

lbool_ShowCodeString = value

End Set

End Property

Public Property CodeStringFont() As Font

Get

Return lfnt_CodeStringFont

End Get

Set(ByVal value As Font)

lfnt_CodeStringFont = value

End Set

End Property

Private lfnt_C39Font As Font

Private lint_C39FontSize As Decimal = 12

Private lstg_C39FontFileName As String

Private lstg_C39FontFamilyName As String

Public Property FontFileName() As String

Get

Return lstg_C39FontFileName

End Get

Set(ByVal value As String)

lstg_C39FontFileName = value

End Set

End Property

Public Property FontFamilyName()

Get

Return lstg_C39FontFamilyName

End Get

Set(ByVal value)

lstg_C39FontFamilyName = value

End Set

End Property

Public Property FontSize() As Decimal

Get

Return lint_C39FontSize

End Get

Set(ByVal value As Decimal)

lint_C39FontSize = value

End Set

End Property

Private ReadOnly Property Code39Font() As Font

Get

If lfnt_C39Font Is Nothing Then

Dim pfc As PrivateFontCollection = New PrivateFontCollection

pfc.AddFontFile(lstg_C39FontFileName)

Dim family As FontFamily = New FontFamily(lstg_C39FontFamilyName, pfc)

lfnt_C39Font = New Font(family, lint_C39FontSize)

End If

Return lfnt_C39Font

End Get

End Property

Public Function GenerateBarcode(ByVal barcode As String) As Bitmap

Dim bcodeWidth As Integer = 0

Dim bcodeHeight As Integer = 0

Dim bcodeBitmap As Bitmap = CreateImageContainer(barcode, bcodeWidth, bcodeHeight)

Dim objGraphics As Graphics = Graphics.FromImage(bcodeBitmap)

objGraphics.FillRectangle(New SolidBrush(Color.White), New Rectangle(0, 0, bcodeWidth, bcodeHeight))

Dim vpos As Integer = 0

If lstg_TitleString <> "" Then

objGraphics.DrawString(lstg_TitleString, lfnt_TitleFont, New SolidBrush(Color.Black), XCentered((CInt(lint_TitleSize.Width)), bcodeWidth), vpos)

vpos += ((CInt(lint_TitleSize.Height)) + lint_ItemSepHeight)

End If

objGraphics.DrawString(barcode, Code39Font, New SolidBrush(Color.Black), XCentered(CInt(lint_BarCodeSize.Width), bcodeWidth), vpos)

If lbool_ShowCodeString Then

vpos += ((CInt(lint_BarCodeSize.Height)))

objGraphics.DrawString(barcode, lfnt_CodeStringFont, New SolidBrush(Color.Black), XCentered(CInt(lint_CodeStringSize.Width), bcodeWidth), vpos)

End If

Return bcodeBitmap

End Function

Private Function CreateImageContainer(ByVal barcode As String, ByRef bcodeWidth As Integer, ByRef bcodeHeight As Integer)

Dim objGraphics As Graphics

Dim tmpBitmap As Bitmap = New Bitmap(1, 1, PixelFormat.Format32bppArgb)

objGraphics = Graphics.FromImage(tmpBitmap)

If lstg_TitleString <> "" Then

lint_TitleSize = objGraphics.MeasureString(lstg_TitleString, lfnt_TitleFont)

bcodeWidth = CInt(lint_TitleSize.Width)

bcodeHeight = CInt(lint_TitleSize.Height + lint_ItemSepHeight)

End If

lint_BarCodeSize = objGraphics.MeasureString(barcode, Code39Font)

bcodeWidth = Max(bcodeWidth, CInt(lint_BarCodeSize.Width))

bcodeHeight += CInt(lint_BarCodeSize.Height)

If lbool_ShowCodeString Then

lint_CodeStringSize = objGraphics.MeasureString(barcode, lfnt_CodeStringFont)

bcodeWidth = Max(bcodeWidth, CInt(lint_CodeStringSize.Width))

bcodeHeight += lint_ItemSepHeight + CInt(lint_CodeStringSize.Height)

End If

objGraphics.Dispose()

tmpBitmap.Dispose()

Return (New Bitmap(bcodeWidth, bcodeHeight, PixelFormat.Format32bppArgb))

End Function

Private Function Max(ByVal v1 As Integer, ByVal v2 As String)

If v1 > v2 Then

Return v1

Else

Return v2

End If

End Function

Private Function XCentered(ByVal localWidth As Integer, ByVal globalWidht As Integer) As Integer

Return ((globalWidht - localWidth) / 2)

End Function

End Class

End Namespace

-Barcode Service: BarCodeService.asmx

Imports System.Web

Imports System.Web.Services

Imports System.Web.Services.Protocols

Imports System

Imports System.Collections

Imports System.ComponentModel

Imports System.Data

Imports System.Diagnostics

Imports System.Drawing

Imports System.Drawing.Imaging

Imports System.IO

Imports System.Configuration

<WebService(Namespace:="http://tempuri.org/")> _

<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

Public Class BarCodeService

Inherits System.Web.Services.WebService

<WebMethod()> _

Public Function HelloWorld() As String

Return "Hello World"

End Function

<WebMethod()> _

Public Function Code39(ByVal code As String, ByVal barSize As Integer, ByVal showCodeString As Boolean, ByVal title As String) As Byte()

Dim c39 As Barcodes.BarCode39 = New Barcodes.BarCode39

'Create stream....

Dim ms As MemoryStream = New MemoryStream()

c39.FontFamilyName = ConfigurationSettings.AppSettings.Get("BarCodeFontFamily")

c39.FontFileName = ConfigurationSettings.AppSettings.Get("BarCodeFontFile")

c39.FontSize = barSize

c39.ShowCodeString = showCodeString

If title + "" <> "" Then

c39.Title = title

End If

Dim objBitmap As Bitmap = c39.GenerateBarcode(code)

objBitmap.Save(ms, ImageFormat.Png)

'return bytes....

Return ms.GetBuffer()

End Function

End Class

-Web Client Call Web Service Generate Barcode

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim barCodeGen As New BarCodeLive.BarCodeService

barCodeGen.Credentials = System.Net.CredentialCache.DefaultCredentials

Dim barsize As Integer

barsize = System.Convert.ToInt32(Me.TextBox_BarCodeFontSize.Text)

Dim imgBarcode As System.Byte() = barCodeGen.Code39(Me.TextBox_BarCodeString.Text, barsize, Me.CheckBox_TitleShow.Checked, Nothing)

Dim memStream As MemoryStream = New MemoryStream(imgBarcode)

Dim xx As New System.Drawing.Bitmap(memStream)

' xx.SetPixel(10, 20, System.Drawing.Color.Empty)

margin: 0cm 0cm 0pt; text-align: le

分享到:
评论

相关推荐

    OnBarcode.Barcode.WinForms.rar

    标题 "OnBarcode.Barcode.WinForms.rar" 暗示这是一个与条形码生成相关的软件开发组件,主要用于Windows Forms应用程序。此组件可能提供了在Windows桌面应用中创建、显示和打印条形码的功能。 描述中的 "\OnBarcode...

    spire.barcode.free-2.6.2.jar

    二维码生成组件 spire.barcode.free 最新版得.

    unity二维码识别最新插件QR CodeBarcode4.9.rar

    QR CodeBarcode Scanner and Generator4.9 最新版本 QR CodeBarcode Scanner Generator是一个高效的代码扫描器和生成器工具,它运行跨平台,支持扫描QRCode,Code_128,Code_93,Code_39,EAN_13,EAN_8,Aztec代码...

    Symbol EDA设备条码读码Demo2

    上一版本“Symbol EDA设备条码读码Demo”读码API命名空间为"Symbol.Barcode.Design.dll" 4. 发声API命名空间为“Symbol.Audio.dll” 5. 适合于MC50、MC3090\MC3190、MC55xx、MC7094、MC9090等设备 Email:baifucn...

    spire.barcode-2.6.0.jar

    spire.barcode-2.6.0 最新版 二维码生成组件.

    Barcode Toolbox

    **Barcode Toolbox** 是一款专业的二维码制作插件,主要用于生成、编辑和管理二维码。这款工具以其英文界面和易于使用的特性而闻名,即便对于初次使用者来说,也能快速掌握基本操作。在这款插件的帮助下,用户可以...

    BarCode.rar一维条形码生成DLL

    在给定的`BarCode.rar`压缩包中,包含的文件有`BarCode.dll`、`BarCode.pdb`、`BarCode.dll.refresh`和`BarCode.xml`,这些都是开发过程中常见的文件类型,对于理解和使用这个DLL至关重要。 `BarCode.dll`是一个...

    barcode_.zip

    在本例中,`barcode.js` 可能提供了API接口,允许开发者通过简单的调用来生成各种类型的条形码,如EAN-13、UPC-A或Code 128等。这些条形码可以基于文本数据,比如产品代码或序列号。 生成条形码的过程通常包括以下...

    PHP BARCODE(条码库,jpgraph_barcode.php)

    Jpgraph的条码库文件jpgraph_barcode.php,包含在JpGraph Pro-version中,是制作页面条码的利器!详情请从http://jpgraph.net/中下载相关文档。

    jquery barcode js 包+示例

    - `jquery.barcode.js` - `jQuery Barcode` 库的源代码文件,用于在页面中引入并使用。 - 示例 HTML 页面 - 展示如何在实际项目中集成和使用这个库的示例代码。 - 可能还有其他资源文件,如 CSS 样式表或图片,用于...

    Barcode studio.zip

    标题"Barcode studio.zip"表明这是一个关于条形码生成工具的软件包,名为"Barcode Studio",并且是以压缩文件(zip格式)的形式存在。这通常意味着它包含源代码、编译文件或其他相关资源,供用户在特定环境中(如...

    VFP_Barcode_Example.zip_BARCODE VFP_barcode_barcode.o_vfp_vfp ba

    "BARCODE VFP_barcode_barcode.o_vfp_vfp ba"暗示了这个例子可能涉及到在VFP中实现条形码生成,以及可能用到的特定对象如“barcode.o”,这可能是一个编译后的条形码生成类或函数。 描述中提到的"Sample of Barcode...

    BarcodeScanner.zip项目安卓应用源码下载

    【 BarcodeScanner.zip 项目安卓应用源码解析】 `BarcodeScanner.zip` 是一个针对安卓平台的条形码扫描应用的源代码包。这个项目为开发者提供了一个实现条形码读取功能的实例,对于学习和研究Android应用开发,尤其...

    Free Spire.Barcode for Java版本: 5.1.1

    Free Spire.Barcode for Java是专为Java开发者设计的一款强大且免费的条形码生成库。这个版本5.1.1提供了丰富的功能和优化,旨在帮助开发者轻松地在Java应用程序中集成条形码生成和识别功能。以下是关于Free Spire....

    spire.barcode_1.4.zip

    《Spire.Barcode:生成条形码与二维码的专业工具》 在信息技术日益发达的今天,条形码和二维码已经成为商品管理、数据交换以及信息化服务的重要组成部分。Spire.Barcode是一款强大的条形码和二维码生成软件,它为...

    BarCode.dll

    《BarCode.dll:掌握条形码生成核心技术》 在信息技术高度发达的今天,条形码作为数据快速识别的重要工具,已经广泛应用于零售、物流、仓储等多个领域。在编程开发中,生成条形码的能力是不可或缺的,而BarCode.dll...

    免费.Net条形码组件:Spire.BarCode for .NET

    Spire.BarCode for .Net 是一款专业的免费条形码组件,专为.Net(C#, VB.NET, ASP.NET)开发人员设计,用于生成和读取一维和二维条形码。使用Spire.BarCode,开发编程人员可以迅速轻松地为.Net应用软件(ASP.NET, ...

    BarcodeScanner.rar

    【标题】"BarcodeScanner.rar" 是一个压缩包文件,很可能包含了一个用于扫描和处理条形码的软件项目或库的源代码。从标题来看,我们可以推测这个资源是为开发者提供的,目的是供他们学习、参考或者集成到自己的应用...

    2D.Barcode.FMX.Components.v.4.1.0.751.XE2-XE10.1

    二维码控件(FMX)

    BarCode_visualbasic_VB中barcode_vb无barcode控件.zip

    "BarCode_visualbasic_VB中barcode_vb无barcode控件.zip" 提供的源码正是解决这个问题的一个实例。下面,我们将深入探讨如何在VB中实现条形码的生成,以及该压缩包中的源码可能包含的内容。 首先,让我们理解条形码...

Global site tag (gtag.js) - Google Analytics