Search code examples
vb.netbarcodecode128

How to generate a barcode with embedded text using GenCode128


I am trying to generate a barcode with its text embedded under the very barcode itself but I can only generate the barcode without the text embedded on it.

Here is my code :

Public Function process_printbarcode(lbl169 As Label)

    Dim length As Integer = 1

    Dim mybarcode As Image = Code128Rendering.MakeBarcodeImage(lbl169.Text.ToString, Integer.Parse(length.ToString()), False)

    Admin_Menu.PictureBox3.Image = mybarcode

    Return True

End Function

Solution

  • GenCode128 does not include an option to display the encoded value below the barcode as part of the image. You could use the Bitmap and Graphics classes to modify the image and add text to it, but I think it would be easier to just use a different DLL that comes with that functionality. One I've personally used is BarcodeLib. You can add it to your project in a few ways:

    1. Add it as a Nuget Package by running Install-Package BarcodeLib -Version 1.0.0.23 in your package manager console
    2. Download the project's GitHub source-code and build the BarcodeLib.dll yourself

    Either way, just add it as a reference in your solution. BarcodeLib has got a lot more barcode parameters available for you to set (and several other encoding types as well), and it's very easy to create them:

    Private Function Code128Image(ByVal value As String, _
                          Optional height As Integer = 100, _
                          Optional barWidth As Integer = 1, _
                          Optional labelIncluded As Boolean = True, _
                          Optional labelPosition As BarcodeLib.LabelPositions = LabelPositions.BOTTOMCENTER, _
                          Optional barcodeRotation As System.Drawing.RotateFlipType = System.Drawing.RotateFlipType.RotateNoneFlipNone) _
                      As System.Drawing.Image
        Using barcodeImage As New BarcodeLib.Barcode
            With barcodeImage
                .Height = height
                .BarWidth = barWidth
                .IncludeLabel = labelIncluded
                .LabelPosition = labelPosition
                .RotateFlipType = barcodeRotation
                Return .Encode(BarcodeLib.TYPE.CODE128, value)
            End With
        End Using
    End Function
    

    Calling Admin_Menu.PictureBox3.Image = Code128Image("123456789") would get you:

    code128img.png