- Download Demo project-55 KB
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 wowould generate the barcode dynamically and return it streamlined as an image.
This article describes the solution that I have 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:
- Load the barcode font
- Create an image object
- Draw a string into that image using a code39 barcode font
- 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:
Collapse Copy code Code
// Create a private font collectionobjectprivatefontcollection
PFC = New Privatefontcollection ();
// Load in the temporary barcode
Fontpfc. addfontfile ( " C: \ barcodefont \ free3of9. TTF " );
// Select the font family to usefontfamily
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 usingMeasurestring
Method:
Collapse Copy code Code
// 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 typeBarcodesize
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 barcode variable holds the barcode string, the required code is:
Collapse Copy code
//Draw the barcodeObjgraphics. drawstring (barcode, code39font,NewSolidbrush (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:
Collapse Copy code
//Create stream ....
Memorystream MS =NewMemorystream ();
//Save the image to the streamObjbitmap.
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:
Collapse Copy code
Code
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 configureWeb. configFile in order to specify where your barcode font is located. Search for the following section and make your changes accordingly.
Collapse Copy code<Appsettings><Add Key="Barcodefontfile" Value="C: \ temp \ font \ free3of9. TTF" /><Add Key="Barcodefontfamily" Value="Free 3 of 9" /></Appsettings>
And that's all folks. Hope you ve enjoyed it.
Best regards,
Rui Miguel Barbosa