C # barcode generator WebService

Source: Internet
Author: User
    • 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:

    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:

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 usingMeasurestringMethod:

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 typeBarcodesizeHas 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.