Click here to Skip to main content
15,881,380 members
Articles / Database Development / SQL Server / SQL Server 2008R2

How to Embed Barcodes in Your SSRS Report

Rate me:
Please Sign up or sign in to vote.
4.79/5 (31 votes)
27 Jun 2014CPOL4 min read 314.6K   5.7K   33   81
How to use barcodelib generated Barcodes in SSRS (consider Barcode fonts don't work in runtime)

Introduction

The following excerpts will show how to successfully embed barcodes in your SSRS reports (similar to Crystal Reports but somewhat different). Using Barcode fonts is nice, however they won't work on the reports published on the SSRS Reports server. Furthermore, Barcode Fonts can be quite expensive. Thankfully to an excellent article, the world is a far better place now.

To proceed with the below steps, you need to download the binary barcodelib from his article.

Background

So I was struggling to get it to work using first free Barcode fonts, which failed as mentioned in the introduction. 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 of giving 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, 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

Image 1

another reference to the barcodelib.dll in the report servers' bin folder using the browse tab.

Image 2

Then, add the Class Name "Barcodelib.Barcode" and Instance Name "bar".

It should now look like this:

Image 3

in the Code section of the report Properties, you add now the following code:

VB.NET
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 400x30) maybe to your expectations. Playing around trial and error, have 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.

Image 4

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:Image 5

Set the image source to be "Database", MIME type "Image/bmp" and set the expression for field to the following:

Image 6

"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.

VBScript
First(Fields!myfield.Value)

The report's preview will generate 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 maybe issues when using VS2012, see this link.

There are plenty of other very interesting articles on this site on the subject matter of barcodes, have a look!

History

  • 2014-09-09: Added the hint about the VS2012 issue when embedding custom libraries
  • 2014-06-27: Added some more details on the points of interests regarding printing of the barcodes

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Including symbols in barcode not working Pin
Member 1136997714-Jan-15 5:30
Member 1136997714-Jan-15 5:30 
GeneralRe: Including symbols in barcode not working Pin
Philipp Katscher9-Feb-15 17:55
professionalPhilipp Katscher9-Feb-15 17:55 
GeneralRe: Including symbols in barcode not working Pin
Member 1172037428-May-15 5:19
Member 1172037428-May-15 5:19 
QuestionFunction 'Convert' doesn't return a value on all code paths Pin
LynW21-Dec-14 9:51
LynW21-Dec-14 9:51 
AnswerRe: Function 'Convert' doesn't return a value on all code paths Pin
Philipp Katscher12-Jan-15 11:57
professionalPhilipp Katscher12-Jan-15 11:57 
QuestionDisplay barcodes for database columns Pin
Varma0912-Nov-14 19:42
Varma0912-Nov-14 19:42 
AnswerRe: Display barcodes for database columns Pin
Philipp Katscher13-Nov-14 12:14
professionalPhilipp Katscher13-Nov-14 12:14 
GeneralRe: Display barcodes for database columns Pin
Varma0916-Nov-14 15:53
Varma0916-Nov-14 15:53 
Hi Philipp,

Thanks for understanding my requirement and for your quick response.

Before logging this question i did took the matrix and took the image inside the cell and passed the field value as expression
=Code.Convert(Fields!AccountNumber.Value)

But the preview says there is an error: The value expression for the image 'Barcode' contains an error.And Code is not declared. when i saw my report properties code and references are not showing though i have added already for another report(Barcode downloaded report) to the same project.

Now i am able to generate barcode for each account type. Since i am new to SSRS i missed the logic to add reference for each report.

Thank you so much i found your article very helpful and achieve my task very easily. Thanks Philipp

modified 16-Nov-14 22:04pm.

QuestionUnable to find BarcodeLib error message Pin
Member 1112410813-Oct-14 11:43
Member 1112410813-Oct-14 11:43 
AnswerRe: Unable to find BarcodeLib error message Pin
Philipp Katscher12-Jan-15 11:51
professionalPhilipp Katscher12-Jan-15 11:51 
GeneralRe: Unable to find BarcodeLib error message Pin
Member 86482887-Aug-15 3:13
Member 86482887-Aug-15 3:13 
GeneralRe: Unable to find BarcodeLib error message Pin
Member 864828810-Aug-15 4:53
Member 864828810-Aug-15 4:53 
QuestionImage wont display on deployed server Pin
Member 109905629-Sep-14 7:44
Member 109905629-Sep-14 7:44 
AnswerRe: Image wont display on deployed server Pin
Philipp Katscher9-Sep-14 13:40
professionalPhilipp Katscher9-Sep-14 13:40 
GeneralRe: Image wont display on deployed server Pin
Member 1099056210-Sep-14 4:33
Member 1099056210-Sep-14 4:33 
GeneralRe: Image wont display on deployed server Pin
Member 1099056210-Sep-14 8:08
Member 1099056210-Sep-14 8:08 
GeneralRe: Image wont display on deployed server Pin
Philipp Katscher10-Sep-14 14:30
professionalPhilipp Katscher10-Sep-14 14:30 
QuestionError Pin
davedave1235-Sep-14 4:35
davedave1235-Sep-14 4:35 
AnswerRe: Error Pin
Philipp Katscher7-Sep-14 22:04
professionalPhilipp Katscher7-Sep-14 22:04 
GeneralRe: Error Pin
davedave1238-Sep-14 12:23
davedave1238-Sep-14 12:23 
GeneralRe: Error Pin
Philipp Katscher8-Sep-14 19:31
professionalPhilipp Katscher8-Sep-14 19:31 
GeneralRe: Error Pin
Philipp Katscher8-Sep-14 19:35
professionalPhilipp Katscher8-Sep-14 19:35 
GeneralMy Vote of 5 Pin
Mubin M. Shaikh20-Jul-14 21:45
professionalMubin M. Shaikh20-Jul-14 21:45 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA20-Jul-14 4:40
professionalȘtefan-Mihai MOGA20-Jul-14 4:40 
GeneralMy vote of 5 Pin
MB Seifollahi24-Jun-14 22:14
professionalMB Seifollahi24-Jun-14 22:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.