views:

5014

answers:

9

I want to create Code39 encoded barcodes from my application.

I know I can use a font for this, but I'd prefer not to as I'd have to register the font on the server and I've had some pretty bad experiences with that.

An example of what I've produced after asking this question is in the answers

+3  A: 

If you choose Code39, you could probably code up from this code I wrote

http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/03/24/code-39-barcode-generation.aspx

I wrote it to use our toolkit for image generation, but you could rewrite it to use .NET Image/Graphics pretty easily.

Lou Franco
Thanks, I have been in contact with the scanning people an Code39 is fine. I used your Javascript example to build a .Net version, as your link uses a dll that I don't have.http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/04/25/writing-code-39-barcodes-with-javascript.aspx
sebastiaan
Will post the solution soon.
sebastiaan
cool -- yes, I used our product to make the image, but it can be done with .NET Image/Bitmap too -- as I guess you are doing.
Lou Franco
A: 

At my last job I worked with a couple different libraries in vb.net for this. We had one, and moved to a different one. I can't remember their names (I'd recognize them again if I saw them), but I do know that both were for-pay, we evaluated several different components at the time of the switch, and I think that included a free one. We were a very small shop and very cost sensitive, so if the free component were any good at all you can bet we would have used it (I think we needed 128b support, and it only handled code39).

I also remember that reason we switched was that it was at the same time we moved from .Net 1.1 to .Net 2.0, and the first component was too slow making the transition.

So, in summary, there is something out there, but it wasn't any good 3 years ago. Hopefully someone else can come along and fill in some actual names.

Joel Coehoorn
+2  A: 

Here's an open source barcode rendering library for .NET languages: http://www.codeplex.com/BarcodeRender

It can render some usual encodings.

The license looks benign, and it seems to be usable in both open source and commercial apps (however, IANAL, you might want to check its license yourself.)

Here's another one, also open source, using the Apache 2.0 license: http://sourceforge.net/projects/onecode/

Generally, when you know from the start you're looking for open source components, it's better to bypass Google and directly start searching on SourceForge (it's got a wonderful filtering system for search results, you can filter by language, which is probably of interest to you) or on Microsoft's CodePlex (where choice is usually more limited, but there you go.)

Mihai Limbășan
Thanks, BarcodeRender seems to be an abandoned product with serious bugs. Onecode only supports US mail postal code barcodes.So far, I haven't been able to find anything else on CodePlex or SourceForge.
sebastiaan
+5  A: 

I don't know about libraries -- all of the barcode work I've done has been with barcode fonts. Check out free 3-of-9 if you're using the "3 of 9" format.

Caveats of 3-of-9:

make sure all text is in upper case start and end each barcode with an asterisk

Danimal
Additional caveat: It isn't compressed so barcode widths can get very long very quickly! However, +1 because it's so darn easy to use!
Doug L.
A: 

If you render client side then the font can reside on a workstation. This way you can use 3-of-9. I've used 3-of-9 in several projects and the simplest solution for you.

David Robbins
+4  A: 

This is my current codebehind, with lots of comments:

Option Explicit On
Option Strict On

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Bitmap
Imports System.Drawing.Graphics
Imports System.IO

Partial Public Class Barcode
 Inherits System.Web.UI.Page
 'Sebastiaan Janssen - 20081001 - TINT-30584
 'Most of the code is based on this example: 
 'http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/04/25/writing-code-39-barcodes-with-javascript.aspx-generation.aspx
 'With a bit of this thrown in:
 'http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/03/24/code-39-barcode

 Private _encoding As Hashtable = New Hashtable
 Private Const _wideBarWidth As Short = 8
 Private Const _narrowBarWidth As Short = 2
 Private Const _barHeight As Short = 100

 Sub BarcodeCode39()
  _encoding.Add("*", "bWbwBwBwb")
  _encoding.Add("-", "bWbwbwBwB")
  _encoding.Add("$", "bWbWbWbwb")
  _encoding.Add("%", "bwbWbWbWb")
  _encoding.Add(" ", "bWBwbwBwb")
  _encoding.Add(".", "BWbwbwBwb")
  _encoding.Add("/", "bWbWbwbWb")
  _encoding.Add("+", "bWbwbWbWb")
  _encoding.Add("0", "bwbWBwBwb")
  _encoding.Add("1", "BwbWbwbwB")
  _encoding.Add("2", "bwBWbwbwB")
  _encoding.Add("3", "BwBWbwbwb")
  _encoding.Add("4", "bwbWBwbwB")
  _encoding.Add("5", "BwbWBwbwb")
  _encoding.Add("6", "bwBWBwbwb")
  _encoding.Add("7", "bwbWbwBwB")
  _encoding.Add("8", "BwbWbwBwb")
  _encoding.Add("9", "bwBWbwBwb")
  _encoding.Add("A", "BwbwbWbwB")
  _encoding.Add("B", "bwBwbWbwB")
  _encoding.Add("C", "BwBwbWbwb")
  _encoding.Add("D", "bwbwBWbwB")
  _encoding.Add("E", "BwbwBWbwb")
  _encoding.Add("F", "bwBwBWbwb")
  _encoding.Add("G", "bwbwbWBwB")
  _encoding.Add("H", "BwbwbWBwb")
  _encoding.Add("I", "bwBwbWBwb")
  _encoding.Add("J", "bwbwBWBwb")
  _encoding.Add("K", "BwbwbwbWB")
  _encoding.Add("L", "bwBwbwbWB")
  _encoding.Add("M", "BwBwbwbWb")
  _encoding.Add("N", "bwbwBwbWB")
  _encoding.Add("O", "BwbwBwbWb")
  _encoding.Add("P", "bwBwBwbWb")
  _encoding.Add("Q", "bwbwbwBWB")
  _encoding.Add("R", "BwbwbwBWb")
  _encoding.Add("S", "bwBwbwBWb")
  _encoding.Add("T", "bwbwBwBWb")
  _encoding.Add("U", "BWbwbwbwB")
  _encoding.Add("V", "bWBwbwbwB")
  _encoding.Add("W", "BWBwbwbwb")
  _encoding.Add("X", "bWbwBwbwB")
  _encoding.Add("Y", "BWbwBwbwb")
  _encoding.Add("Z", "bWBwBwbwb")
 End Sub

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  BarcodeCode39()
  Dim barcode As String = String.Empty
  If Not IsNothing(Request("barcode")) AndAlso Not (Request("barcode").Length = 0) Then
   barcode = Request("barcode")
   Response.ContentType = "image/png"
   Response.AddHeader("Content-Disposition", String.Format("attachment; filename=barcode_{0}.png", barcode))

   'TODO: Depending on the length of the string, determine how wide the image will be
   GenerateBarcodeImage(250, 140, barcode).WriteTo(Response.OutputStream)
  End If
 End Sub

 Protected Function getBCSymbolColor(ByVal symbol As String) As System.Drawing.Brush
  getBCSymbolColor = Brushes.Black
  If symbol = "W" Or symbol = "w" Then
   getBCSymbolColor = Brushes.White
  End If
 End Function

 Protected Function getBCSymbolWidth(ByVal symbol As String) As Short
  getBCSymbolWidth = _narrowBarWidth
  If symbol = "B" Or symbol = "W" Then
   getBCSymbolWidth = _wideBarWidth
  End If
 End Function

 Protected Overridable Function GenerateBarcodeImage(ByVal imageWidth As Short, ByVal imageHeight As Short, ByVal Code As String) As MemoryStream
  'create a new bitmap
  Dim b As New Bitmap(imageWidth, imageHeight, Imaging.PixelFormat.Format32bppArgb)

  'create a canvas to paint on
  Dim canvas As New Rectangle(0, 0, imageWidth, imageHeight)

  'draw a white background
  Dim g As Graphics = Graphics.FromImage(b)
  g.FillRectangle(Brushes.White, 0, 0, imageWidth, imageHeight)

  'write the unaltered code at the bottom
  'TODO: truely center this text
  Dim textBrush As New SolidBrush(Color.Black)
  g.DrawString(Code, New Font("Courier New", 12), textBrush, 100, 110)

  'Code has to be surrounded by asterisks to make it a valid Code39 barcode
  Dim UseCode As String = String.Format("{0}{1}{0}", "*", Code)

  'Start drawing at 10, 10
  Dim XPosition As Short = 10
  Dim YPosition As Short = 10

  Dim invalidCharacter As Boolean = False
  Dim CurrentSymbol As String = String.Empty

  For j As Short = 0 To CShort(UseCode.Length - 1)
   CurrentSymbol = UseCode.Substring(j, 1)
   'check if symbol can be used
   If Not IsNothing(_encoding(CurrentSymbol)) Then
    Dim EncodedSymbol As String = _encoding(CurrentSymbol).ToString

    For i As Short = 0 To CShort(EncodedSymbol.Length - 1)
     Dim CurrentCode As String = EncodedSymbol.Substring(i, 1)
     g.FillRectangle(getBCSymbolColor(CurrentCode), XPosition, YPosition, getBCSymbolWidth(CurrentCode), _barHeight)
     XPosition = XPosition + getBCSymbolWidth(CurrentCode)
    Next

    'After each written full symbol we need a whitespace (narrow width)
    g.FillRectangle(getBCSymbolColor("w"), XPosition, YPosition, getBCSymbolWidth("w"), _barHeight)
    XPosition = XPosition + getBCSymbolWidth("w")
   Else
    invalidCharacter = True
   End If
  Next

  'errorhandling when an invalidcharacter is found
  If invalidCharacter Then
   g.FillRectangle(Brushes.White, 0, 0, imageWidth, imageHeight)
   g.DrawString("Invalid characters found,", New Font("Courier New", 8), textBrush, 0, 0)
   g.DrawString("no barcode generated", New Font("Courier New", 8), textBrush, 0, 10)
   g.DrawString("Input was: ", New Font("Courier New", 8), textBrush, 0, 30)
   g.DrawString(Code, New Font("Courier New", 8), textBrush, 0, 40)
  End If

  'write the image into a memorystream
  Dim ms As New MemoryStream

  Dim encodingParams As New EncoderParameters
  encodingParams.Param(0) = New EncoderParameter(Encoder.Quality, 100)

  Dim encodingInfo As ImageCodecInfo = FindCodecInfo("PNG")

  b.Save(ms, encodingInfo, encodingParams)

  'dispose of the object we won't need any more
  g.Dispose()
  b.Dispose()

  Return ms
 End Function

 Protected Overridable Function FindCodecInfo(ByVal codec As String) As ImageCodecInfo
  Dim encoders As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders
  For Each e As ImageCodecInfo In encoders
   If e.FormatDescription.Equals(codec) Then Return e
  Next
  Return Nothing
 End Function
End Class
sebastiaan
Too bad I can't accept my own answer ;-)
sebastiaan
A: 

are you printing to a standard printer or an actual barcode printer (zebra or datamax)? both zebra and datamax have their own scripting languages - actually more like markup languages. ZPL and DPL respectively. I like zebra's more and their documentation is much cleaner.

If you don't have a legitimate barcode printer, I suggest purchasing one and do the following.... (it'll be much cleaner than trying to work out building little image blocks and painting them to simulate a barcode font)

both allow you great flexibility and you can let the printer handle creating the actual barcode image.

have your program send a customized ZPL/DPL script that includes the values that you want to have printed as barcodes to the printer via ftp. basically, you just "put" a text file that contains the script to the IP of the printer and the printer takes care of the font.

+1  A: 

The iTextSharp library, while ostensibly for creating creating PDFs, also has a barcode generation library that includes Code39.

Once you add a reference to the DLL, it's as simple as:

Barcode39 code39 = new Barcode39();
code39.Code = "Whatever You're Encoding";

Oops, that's C#, but you get the idea. Once created, you can render an image in just about any image format and use it as you wish.

CMPalmer
A: 

Hi i am very much new to vb.net can anyone please help me to generate barcode (code 39) or provide a code so that i could study it.

The code is right here.. http://stackoverflow.com/questions/149379/how-to-generate-code39-barcodes-in-vb-net/156784#156784
sebastiaan
Thanks sebastiaan . i have tried out this code but it has errors i am not able to take out the errors please help me.
Sorry, without a description of what errors I am not going to be able to help you. Maybe you should just start a new topic for it?
sebastiaan