Click here to Skip to main content
6,295,667 members and growing! (15,382 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

C# Barcode Generator WebService

By Rui Miguel Barbosa

Create a code39 barcode using a webservice in C#
C#, VB 6.NET 1.0, Win2K, WinXP, ASP.NET, Visual Studio, Dev
Posted:31 Mar 2003
Views:211,313
Bookmarked:167 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
51 votes for this article.
Popularity: 7.53 Rating: 4.41 out of 5
3 votes, 5.9%
1

2
2 votes, 3.9%
3
9 votes, 17.6%
4
37 votes, 72.5%
5

Sample Image - barcodegen.jpg

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>

And that�s all folks. Hope you�ve enjoyed it.

Best regards,
Rui Miguel Barbosa

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Rui Miguel Barbosa


Member

Occupation: Web Developer
Location: Portugal Portugal

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 40 (Total in Forum: 40) (Refresh)FirstPrevNext
GeneralScanner couldn't scan the printed barcodes.., (scanner says it supports code39) Pinmembersaurabhnijhawan198721:25 16 Jun '09  
Generalgetting an error while running Pinmemberkutti krishnan kodoth20:06 12 Jun '09  
GeneralScanner Couldn't read Pinmembermurtaza dhari0:52 10 Feb '09  
GeneralRe: Scanner Couldn't read Pinmembermurtaza dhari1:42 10 Feb '09  
GeneralExtended (ASCII) character support PinmemberMark Rodrigues14:10 25 Oct '08  
QuestionRead Bar Code PinmemberSaurabh Sondhi10:04 7 Oct '08  
GeneralLicense Pinmembervictor.sauermann23:09 26 Sep '08  
AnswerRe: License PinmemberMark Rodrigues13:47 25 Oct '08  
GeneralWill it work for Other Barcode (code 128 A, 128 B, PDF417 etc) PinmemberNETA200313:05 25 Apr '07  
GeneralNice work PinmemberAlexandr_K9:52 27 Sep '06  
News.GetBuffer() is bad PinmemberSteve Hansen10:43 19 Mar '06  
GeneralRe: .GetBuffer() is bad Pinmemberdonperry4:35 21 May '08  
GeneralAntialiasing.. Pinmemberhaphaphaphap5:24 1 Nov '05  
GeneralRe: Antialiasing.. PinmemberDuddy6:19 16 Dec '05  
GeneralRe: Antialiasing.. Pinmemberdapoussin4:21 22 May '06  
GeneralRe: Antialiasing.. PinmemberLeonelCorso9:19 9 Oct '07  
GeneralIDAutomation Barcode Generator Webservice Pinsussidautomation6:24 15 Oct '05  
GeneralIt generates different kind of image PinsussAnonymous21:17 18 Aug '05  
GeneralBarcode is incorrect Pinmembertyc10010:48 8 Oct '04  
Generalabout client side Pinmembercodehobbist200521:15 8 Jun '04  
GeneralProduced Image cannot be scanned?? Pinmemberkthuya23:44 23 May '04  
GeneralRe: Produced Image cannot be scanned?? PinsussMai Lan22:24 19 Jun '05  
GeneralRe: Produced Image cannot be scanned?? Pinmemberdado4923:31 12 Apr '07  
GeneralRe: Produced Image cannot be scanned?? Pinmemberdonperry4:48 21 May '08  
GeneralNot Scanning digits 3 and 6..Help Pinmembersandeep7515:17 2 Apr '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 Mar 2003
Editor: Heath Stewart
Copyright 2003 by Rui Miguel Barbosa
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project