SSRS Creates a bitmap image / Barcode of the data parameter

I have these three serial no in my dataset:

SERIAL_NUMBER
PT00G1J1
PT00G1J0
PT00CAGG

I want to create bar-code for them in SSRS Reports like:

1. Add a image in the tablix and set the Properties like:

Value: =code.CreateBarcodeDBField(IIF(Trim(Fields!SERIAL_NUMBER.Value) = “”, Fields!MFG_SERIAL.Value, Fields!SERIAL_NUMBER.Value) )

2. Right click outside of the design area of report and go to report properties:

3. Code (Collected from unknown source. If I find it, I will add the link here):

    ' Creates a bitmap image of the data parameter
    Public Function CreateBarcodeImage(ByVal data As String) As System.Drawing.Bitmap
        Dim dataIn As String
        'Since this is for Code 3 of 9 only right now, add the leading and trailing asterisks to the data
        dataIn = "*" & data + "*"

        'Create the Bitmap we'll be using
        Dim barCode As New System.Drawing.Bitmap(1, 1)

        'Get a reference to the 3 of 9 barcode font
        Dim threeOfNine As New System.Drawing.Font("Free 3 of 9", 60, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)

        'Get a graphics object to work with
        Dim graphics As System.Drawing.Graphics = graphics.FromImage(barCode)

        'Now we need to get the width and height that our 
        'data will cover after it is rendered with the 3 of 9 font
        Dim dataSize As System.Drawing.SizeF = graphics.MeasureString(dataIn, threeOfNine)

        'Now we base the Bitmap's size off of this data.
        barCode = New System.Drawing.Bitmap(barCode, dataSize.ToSize())

        'Refresh our Graphics object with the new bitmap.
        graphics = graphics.FromImage(barCode)

        'Make the Graphic object's drawing surfact white.
        graphics.Clear(System.Drawing.Color.White)

        'Set the rendering hint to SingleBitPerPixel.
        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel

        'Draw the string onto the Graphics object
        graphics.DrawString(dataIn, threeOfNine, New System.Drawing.SolidBrush(System.Drawing.Color.Black), 0, 0)

        'Force the Graphics object to execute any pending operations.
        graphics.Flush()

        'Dispose of our objects
        threeOfNine.Dispose()
        graphics.Dispose()

        'Return the finished barcode.
        Return barCode

    End Function

    'Creates a barcode image of the incoming data in a format suitable for storing in the database
    Public Function CreateBarcodeDBField(ByVal data As String) As Byte()
        Dim image As System.Drawing.Bitmap
        image = CreateBarcodeImage(data)

        'Save to memory using the Gif format
        Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream()
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)

        Dim imageBytes As Byte() = ms.GetBuffer()
        image.Dispose()
        ms.Close()

        ' read to end
        Return imageBytes
    End Function

Public Function Fred as string
    Return "Fred"
End Function

4. Reference

 
System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

5. Output:

6. Font: As We used the font “Free 3 of 9” so this font need to be available in the report manager Windoes/Font folder. Otherwise it will not render the barcode.

Advertisements
By simplemsexchange Posted in SSRS

8 comments on “SSRS Creates a bitmap image / Barcode of the data parameter

  1. I could not get the code to work because of a few syntax errors. I went to work in Visual studio 2010 and got it to work after making a few modifactions (one of the biggest issues was that CreateBarcodeDBField procedure was trying to save a GIF image when the CreateBarcodeImage was creating a bit-map image). Here’s the modified code:

    ‘ Creates a bitmap image of the data parameter
    Public Function CreateBarcodeImage(data As String) As System.Drawing.Bitmap
    Dim dataIn As String = data

    ‘Create the Bitmap we’ll be using
    Dim barCode As New System.Drawing.Bitmap(1, 1)

    ‘Get a reference to the barcode font
    Dim barcodeFont As New System.Drawing.Font(“IDAutomationC39S”, 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)

    ‘Get a graphics object to work with
    Dim graphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(barCode)

    ‘Now we need to get the width and height that our
    ‘data will cover after it is rendered with the barcode font
    Dim dataSize As System.Drawing.SizeF = graphics.MeasureString(dataIn, barcodeFont)

    ‘Now we base the Bitmap’s size off of this data.
    barCode = New System.Drawing.Bitmap(barCode, dataSize.ToSize())

    ‘Refresh our Graphics object with the new bitmap.
    graphics = System.Drawing.Graphics.FromImage(barCode)

    ‘Make the Graphic object’s drawing surfact white.
    graphics.Clear(System.Drawing.Color.White)

    ‘Set the rendering hint to SingleBitPerPixel.
    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel

    ‘Draw the string onto the Graphics object
    graphics.DrawString(dataIn, barcodeFont, New System.Drawing.SolidBrush(System.Drawing.Color.Black), 0, 0)

    ‘Force the Graphics object to execute any pending operations.
    graphics.Flush()

    ‘Dispose of our objects
    barcodeFont.Dispose()
    graphics.Dispose()

    ‘Return the finished barcode.
    Return barCode
    End Function

    ‘Creates a barcode image of the incoming data in a format suitable for storing in the database
    Public Function CreateBarcodeDBField(data As String) As Byte()
    Dim image As System.Drawing.Bitmap = Nothing
    image = CreateBarcodeImage(data)

    ‘Save to memory using the Gif format
    Dim ms As New System.IO.MemoryStream()
    image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)

    Dim imageBytes As Byte() = ms.GetBuffer()
    image.Dispose()
    ms.Close()

    ‘ read to end
    Return imageBytes
    End Function

  2. For me I see only digit numbers, instead of barcode. I install Free 3 of 9 font type on report server and restart it. What’s the problem?

  3. I have the solution. I have copied the code from Drake, and the font is IDAutomationC39S instead of Free 3 of 9. I change it, and works great. Thanks!

  4. This is so awesome! Just what I needed to get the barcode printing from SSRS.
    And thank you so much for the modified code!!

  5. Hi, I used the code from Drake and changed my font to ‘Free 3 of 9’ but when i print it, the scanner is unable to read it. This scanner can read free 3 of 9 font.

  6. It works now. Thankq. SOmehoe Drake code dint work for me and i made little changed to the code posted in the blog and now it works. Thanks a lot for post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s