SQLServerCentral Article

Linear barcodes in SSRS using the Barcode Image Generation Library

,

The Barcode Image Generation Library was designed to provide a C# class that enables generation of barcode images from a string of data. It currently supports the following 27 linear barcode encoding types:

Bookland/ISBN Code-128C MSI
Codabar Code-93 Pharmacode
Code 11 EAN-13 PostNet
Code 39 EAN-8 Standard 2 of 5
Code 39 Extended FIM Telepen
Code 39 Mod 43 Interleaved 2 of 5 UPC 2 Digit Ext.
Code-128 ITF-14 UPC 5 Digit Ext.
Code-128A JAN-13 UPC-A
Code-128B LOGMARS UPC-E

Initial release was on October 10, 2007. The project has been under active development and refinement ever since.

Attribution

The code used in this article was adapted from these two sources:

Overview

These are the steps required to create an SSRS report that displays linear barcode symbols using the Barcode Image Generation Library

  1. Obtain an existing copy or compile BarcodeLib.dll with a strong name and the PartiallyTrustedCallers attribute*
  2. Install BarcodeLib.dll to the global assembly cache (GAC) using the gacutil.exe utility
  3. Create a new SSRS report that queries a table of test data
  4. Add a reference to the BarcodeLib.dll GAC assembly and to the System.Drawing assembly
  5. Add a custom code function that sends a string, encoding type and dimension parameters to the BarcodeLib.dll assembly and accepts an image byte array in return
  6. Add a field to the report
  7. Drag-and-drop an image componenet to the new field to bring up the image properties dialogue
  8. Change the image source to Database, change the MIME type to image/bmp and add a function that references the custom code function.
  9. Set the size property of the image component to original size
  10. Execute the report to display the data and the barcodes generated from the data

*A strong name is required to insert an assemby into the GAC. SSRS requires assemblies it references to include the PartiallyTrustedCallers attribute.

Resource file content

Unzip the attached Barcode_Resources.zip file and browse to the extracted Barcode_Resources folder in File Explorer.

The BarcodeLibSln, folder contains the C# project used to compile the BarcodeLibTest.exe test executable and the BarcodeLib.dll assembly. (You can load this project into Visual Studio or SharpDevelop and compile them yourself if you wish.) The BarcodeLibTestExe folder contains an executable that can be used to test the output of the Barcode Image Generation Library. The StronglyNamedPartiallyTrustedCallersAssembly folder contains the assembly that will be used to generate barcodes in an SSRS report. The barcode_src.zip file is the original Barcode Image Generation Library archive downloaded from https://www.codeproject.com/KB/graphics/BarcodeLibrary/barcode_src.zip.

Test the Barcode Image Generation Library

Browse to the BarcodeLibTestExe folder to display the BarcodeLib.dll assembly and the BarcodeLibTest.exe application that uses it to generate barcodes.

Double-click the BarcodeLibTest.exe application and click the Encode button to generate a barcode symbol.

You can enter any string you wish into the textbox to generate a symbol for that string. Test the application with the appropriate encoding type and typical character strings you want to convert to barcode symbols. Scan the symbols with your barcode reader of choice to confirm the library is satisfactory for your purposes. (When the barcode image is obviously not scaled correctly, play with the width and height parameters until it is scaled well enough to be read by a barcode scanner.)

Locate the BarcodeLib.dll assembly

Browse to the StronglyNamedPartiallyTrustedCallersAssembly folder to confirm that it is present.

Install the BarcodeLib.dll assembly to the global assembly cache (GAC)

Find gacutil.exe with Agent Ransack or File Explorer.

Copy the path of the appropriate 32- or 64-bit version of gacutil.exe:

C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\x64\gacutil.exe

Open a command console as administrator and change to the StronglyNamedPartiallyTrustedCallersAssembly folder.

Install the assembly to the GAC with the following command:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\x64\gacutil.exe" -i BarcodeLib.dll

Confirm the BarcodeLib assembly has been added to the C:\Windows\assembly folder.

Create test data for the report

Open a new query window in SQL Server Management Studio and paste the following SQL code into it.

BEGIN TRY
DROP TABLE [dbo].[BarcodeTestData]
END TRY
BEGIN CATCH END CATCH
GO 
CREATE TABLE [dbo].[BarcodeTestData](
[EncodingType] [varchar](50) NULL,
[EncodingTypeKey] [varchar](50) NULL,
[Width] [int] NULL,
[Height] [int] NULL,
[DataString] [varchar](50) NULL
) 
GO
INSERT INTO [BarcodeTestData] (EncodingType,EncodingTypeKey,Width,Height,DataString)
SELECT 'Bookland/ISBN','BOOKLAND',400,50,'978654321' UNION
SELECT 'Codabar','Codabar',400,50,'A12345A' UNION
SELECT 'Code 11','CODE11',400,50,'123456789012' UNION
SELECT 'Code 39','CODE39',400,50,'123456789012' UNION
SELECT 'Code 39 Extended','CODE39Extended',400,50,'12345678901234567' UNION
SELECT 'Code 39 Mod 43','CODE39_Mod43',400,50,'123456789012' UNION
SELECT 'Code-128','CODE128',400,50,'01234567890123456789' UNION
SELECT 'Code-128A','CODE128A',400,50,'01234567890123456789' UNION
SELECT 'Code-128B','CODE128B',400,50,'01234567890123456789' UNION
SELECT 'Code-128C','CODE128C',400,50,'01234567890123456789' UNION
SELECT 'Code-93','CODE93',400,50,'01234567890123456789' UNION
SELECT 'EAN-13','EAN13',400,50,'0123456789012' UNION
SELECT 'EAN-8','EAN8',400,50,'12345678' UNION
SELECT 'FIM','FIM',400,50,'A' UNION
SELECT 'Interleaved 2 of 5','Interleaved2of5',400,50,'01234567890123456789' UNION
SELECT 'ITF-14','ITF14',400,50,'01234567890123' UNION
SELECT 'JAN-13','JAN13',400,50,'4912345123459' UNION
SELECT 'LOGMARS','LOGMARS',400,50,'01234567890123456789' UNION
SELECT 'MSI','MSI_Mod10',400,50,'1234567' UNION
SELECT 'Pharmacode','PHARMACODE',400,50,'123456' UNION
SELECT 'PostNet','PostNet',400,50,'01234' UNION
SELECT 'Standard 2 of 5','Standard2of5',400,50,'01234567890123456789' UNION
SELECT 'Telepen','TELEPEN',400,50,'ABC123' UNION
SELECT 'UPC 2 Digit Ext.','UPC_SUPPLEMENTAL_2DIGIT',400,50,'98' UNION
SELECT 'UPC 5 Digit Ext.','UPC_SUPPLEMENTAL_5DIGIT',400,50,'90000' UNION
SELECT 'UPC-A','UPCA',400,50,'123456789012' UNION
SELECT 'UPC-E','UPCE',400,50,'12345678' 
GO
SELECT [EncodingType],[EncodingTypeKey],[DataString],[Width],[Height] FROM [BarcodeTestData] ORDER BY [EncodingType]
GO

Execute the query to create a table named BarcodeTestData that will be used to generate barcodes for all 27 types that the Barcode Image Generation Library supports.

Create and execute the Barcode Image Generation Library Demo SSRS report

Start a new Report Server Project Wizard in Visual Studio with the name Barcode Image Generation Library Demo.

Insert the appropriate connection string information.

Insert the following SQL code into the query string text box.

SELECT [EncodingType],[EncodingTypeKey],[DataString],[Width],[Height]
 FROM [BarcodeTestData]
 ORDER BY [EncodingType]

Select tabular report type.

Move all the available fields to the details displayed fields list.

Enter Barcode Image Generation Library Demo into the report name field and click the finish button.

Copy the following SQL code into the clipboard.

Public Function Convert(Text As String, EncodingTypeKey As String,Width as Integer, Height as Integer) As Byte()
           Dim b As System.Drawing.Bitmap
           Dim Barcode As New BarcodeLib.Barcode
           Barcode.Alignment = BarcodeLib.AlignmentPositions.LEFT
           Barcode.IncludeLabel = False
           Barcode.RotateFlipType = Drawing.RotateFlipType.RotateNoneFlipNone
           If (EncodingTypeKey = "BOOKLAND") 
                b = Barcode.Encode(BarcodeLib.TYPE.BOOKLAND, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "Codabar") 
                b = Barcode.Encode(BarcodeLib.TYPE.Codabar, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "CODE11") 
                b = Barcode.Encode(BarcodeLib.TYPE.CODE11, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "CODE39") 
                b = Barcode.Encode(BarcodeLib.TYPE.CODE39, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "CODE39Extended") 
                b = Barcode.Encode(BarcodeLib.TYPE.CODE39Extended, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "CODE39_Mod43") 
                b = Barcode.Encode(BarcodeLib.TYPE.CODE39_Mod43, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "CODE128") 
                b = Barcode.Encode(BarcodeLib.TYPE.CODE128, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "CODE128A") 
                b = Barcode.Encode(BarcodeLib.TYPE.CODE128A, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "CODE128B") 
                b = Barcode.Encode(BarcodeLib.TYPE.CODE128B, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "CODE128C") 
                b = Barcode.Encode(BarcodeLib.TYPE.CODE128C, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "CODE93") 
                b = Barcode.Encode(BarcodeLib.TYPE.CODE93, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "EAN13") 
                b = Barcode.Encode(BarcodeLib.TYPE.EAN13, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "EAN8") 
                b = Barcode.Encode(BarcodeLib.TYPE.EAN8, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "FIM") 
                b = Barcode.Encode(BarcodeLib.TYPE.FIM, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "Interleaved2of5") 
                b = Barcode.Encode(BarcodeLib.TYPE.Interleaved2of5, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "ITF14") 
                b = Barcode.Encode(BarcodeLib.TYPE.ITF14, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "JAN13") 
                b = Barcode.Encode(BarcodeLib.TYPE.JAN13, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "LOGMARS") 
                b = Barcode.Encode(BarcodeLib.TYPE.LOGMARS, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "MSI_Mod10") 
                b = Barcode.Encode(BarcodeLib.TYPE.MSI_Mod10, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "PHARMACODE") 
                b = Barcode.Encode(BarcodeLib.TYPE.PHARMACODE, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "PostNet") 
                b = Barcode.Encode(BarcodeLib.TYPE.PostNet, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "Standard2of5") 
                b = Barcode.Encode(BarcodeLib.TYPE.Standard2of5, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "TELEPEN") 
                b = Barcode.Encode(BarcodeLib.TYPE.TELEPEN, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "UPC_SUPPLEMENTAL_2DIGIT") 
                b = Barcode.Encode(BarcodeLib.TYPE.UPC_SUPPLEMENTAL_2DIGIT, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "UPC_SUPPLEMENTAL_5DIGIT") 
                b = Barcode.Encode(BarcodeLib.TYPE.UPC_SUPPLEMENTAL_5DIGIT, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "UPCA") 
                b = Barcode.Encode(BarcodeLib.TYPE.UPCA, Text, Width, Height) 
           End If
           If (EncodingTypeKey = "UPCE") 
                b = Barcode.Encode(BarcodeLib.TYPE.UPCE, Text, Width, Height) 
           End If
           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

Click on Report-Report Properties...

Click the code tab and paste the content of the clipboard into the custom code textbox.

Click the references tab followed by the add button then the browse tab and drill down to BarcodeLib.dll in the C:\Windows\assembly folder.

Click the add button again then click the .NET tab to add the System.Drawing assembly.

There should now be two assemblies listed in the references tab.

Right-click on the top of the height column and select insert column-right from the popup menu.

Drag-and-drop an image component to the new column's data field.

Change image source to database and MIME type to image/bmp, then click on the function button of the use this field drop-down list.

Enter the following code into the set expression text box.

=Code.Convert(Fields!DataString.Value.ToString,Fields!EncodingTypeKey.Value.ToString,Fields!Width.Value,Fields!Height.Value)

Click the size tab and click the original size radio button.

Click the preview tab to display the report containing barcodes.

Adjust the scaling factor

By default, most new barcode symbols will be displayed with an incorrect scaling factor, which makes the bars too thick or too thin.

Adjust the height and/or width parameters for new barcode symbost to correct this. Use a barcode scanner app to determine when the scaling factor is acceptable.

Conclusion

The open source Barcode Image Generation Library accurately produces 27 types of linear barcodes. It does not automatically scale barcode images correctly, however, requiring tuning of the height and width parameters. Once the appropriate scaling factor is determined the barcode symbol can be put into production.

Resources

Rate

4.89 (9)

You rated this post out of 5. Change rating

Share

Share

Rate

4.89 (9)

You rated this post out of 5. Change rating