The following excerpts will show how to embed successfully barcodes in your SSRS reports (similar to Crystal Reports but somewhat different). Using Barcode fonts is nice, however won’t work on the reports published on a the SSRS Reports server. Furthermore Barcode Fonts can be quite expensive. Thankfully to Brad Barnhill, who published an excellent article (http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library), the world is a far better place now.
To procede with the below steps, you need to download the binary barcodelib from his article here Download source – 1.1 KB.
So I was struggeling to get it to work using first free Barcode fonts, which failed as mentioned in the intro. You can try and you’ll think it works until you publish the report.
Next I attempted to write some custom code generating a Bitmap, using Graphics.DrawString into it using the Barcode font and returning it as Byte. Great it worked, BUT the image quality deteriorated so dramatically when exporting the report as PDF, because first of all it being a truetype font ….so the barcode rendered and converted and printed etc. was scannable at times but unfortunately the barcode scanner would strike at times, with the image not being good enough to be readable. So using my own code and a free font wasn’t good enough. Plenty of forums and ideas’ later I found Brad’s article and thought to give it a try for it renders the barcodes directly and is not a font conversion to image.
Using the barcodelib as provided by Brad Barnhill seems to perfectly generate barcodes that actually come out in excellent quality even after printing and highly configurable as well, rotating them, with and without labels, all sorts of barcode format etc.
So how to integrate that custom dll stuff in SSRS? Well, see below.
Using the code
First of all you copy the barcodelib.dll (from Brad Barnhill‘s article http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library)
to “Everywhere”. To be more specific (for my example with SSRS 2008 and VS 2008) ->
C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportServer\bin\
C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies
C:\Program Files (x86)\Microsoft Visual Studio 9\Common7\IDE\PrivateAssemblies
Then you add in the Report->Properties references to System.Drawing from the .NET tab and
another reference to the barcodelib.dll in the report servers’s bin folder using the browse tab.
Then add the Class Name “Barcodelib.Barcode” and Instance Name “bar”.
It should now look like this:
in the Code section of the report Properties you add now the following code
Public Function Convert(Text As String) As Byte() Dim b As System.Drawing.Bitmap ' Dim bar As New BarcodeLib.Barcode bar.Alignment = BarcodeLib.AlignmentPositions.LEFT bar.IncludeLabel = False bar.RotateFlipType = Drawing.RotateFlipType.RotateNoneFlipNone b = bar.Encode(BarcodeLib.TYPE.CODE39Extended, Text, 400, 30) Dim bitmapData As Byte() = Nothing Using ms As New System.IO.MemoryStream() b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp) bitmapData = ms.ToArray() End Using Return bitmapData End Function
Do you recognise the instance “bar” from earlier is being used here?
You may replace the settings as you’d like your Barcode formatted (i.e. enhance the function to take parameters if you like), rotated, type of barcode format etc. Check the earlier in the Introduction mentioned article for the barcodelib’s featurette! Can’t thank Brad enough!
And adjust the size (in this sample 400×30) maybe to your expectations. Playing around trial and error, have a fun.
The SSRS image properties (see a little further below) want a byte. So they shall get one, quickly write the bitmap into a memorystream and take the byte. Other methods i.e. using Marshal.Copy may work but would introduce more hustle in the SSRS embedded Code scenario that we are dealing with here.
Now that everything is prepared, we get to the interesting part.
You add an Image to your report to contain the barcode. Go to the Image properties
Set the image source to be “Database”, MIME type “Image/bmp” and set the expression for field to the following:
“Code” accesses the namespace of your embedded code where we called barcodelib to generate the Barcode.
You may replace “*Test1234*” with your Barcode Contents, i.e. from datasource
The report’s preview will generate you a neat barcode, and your deployed/published version of the same on your reports server as well . Too easy, ain’t it?
voilà – all done Now!
Points of Interest
There are a number of things to watch out for.
- Make your image size big enough or allow auto growth, or set to Fit Proportional. If the barcode gets distorted or clipped it may not render readable for a barcodescanner if half of it is missing
- Code39Extended seems to require an asterisk to start and end the barcode. That means your text needs to start with * and end with *. The contents of the read barcode will then be whatever is between the * asterisks. That may apply for some other barcode formats as well.
- Make sure you print your reports with a high resolution so the barcode comes out as it should and not all wash, use color printing as the original barcode image is 24Bit bitmap, and grayscaling may wash it. On my laser printer I use color, one sided and raster printing as options.
- If you deploy your reports to another remote machine, make sure you deployed the barcodelib to that machine’s report servers’ bin directory
There are plenty of other very interesting articles on this site on the subject matter of barcodes, have a look!