现在的位置: 首页 > 综合 > 正文

BarCode WbeService…

2013年10月03日 ⁄ 综合 ⁄ 共 6908字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.