Search code examples
vb.netvisual-studio-2019zxing

RDLC generate barcode using ZXing


I am pretty new to the RDLC report feature, I am looking to generate labels from Product data within a SQL database. When the user opens this Product/Part they are greeted with the information. When the user then clicks a button this will open the Report which will pass the parameters across to the Report in order to generate the label.

enter image description here

        Dim myparam As ReportParameter
        Dim testParameter As New List(Of ReportParameter)

        myparam = New ReportParameter("PartID", "Test")
        testParameter.Add(myparam)

        myparam = New ReportParameter("MRPID", "Test MRP")
        testParameter.Add(myparam)

        myparam = New ReportParameter("PartName", "Test Name")
        testParameter.Add(myparam)

        ReportViewer1.LocalReport.SetParameters(testParameter)

        Dim writer As New BarcodeWriter
        writer.Format = BarcodeFormat.CODE_128
        PictureBox1.Image = writer.Write(MRPID)

        Me.ReportViewer1.RefreshReport()

As you can see, I am using XLing to generate my barcodes, which I have been successful in making work with the 3 lines of code you see above. However, I have no idea how I can pass this or have this generate on the report when ran. The barcode will be generated from the MRPID ie(TV001232). I understand this part is wrong "writer.Write(MRPID)" but I replaced the parameter value with MRPID so you could understand what I am trying to achieve.


Solution

  • Convert your image to Base64 string first using this:

    Public Function ImageToBase64(ByVal image As Image, ByVal format As System.Drawing.Imaging.ImageFormat) As String
        Dim base64String As String = ""
        Using ms As New System.IO.MemoryStream()
            image.Save(ms, format)
            Dim imageBytes As Byte() = ms.ToArray()
            base64String = Convert.ToBase64String(imageBytes)
        End Using
        Return base64String
    End Function
    

    So this:

    myparam = New ReportParameter("MRPID", "Test MRP")
    testParameter.Add(myparam)
    

    Should be like this:

    Dim writer As New BarcodeWriter
    writer.Format = BarcodeFormat.CODE_128
    myparam = New ReportParameter("MRPID", ImageToBase64(writer.Write(MRPID),<THE IMAGE FORMAT OF YOUR IMAGE>))
    testParameter.Add(myparam)
    

    Then, in your report set the following:

    MIMEType = select the correct MIME type from the dropdown list
    Source = Database
    Value = <Expression>
    

    and in the Expression window:

    =System.Convert.FromBase64String(Parameters!MRPID.Value)