65.9K
CodeProject 正在变化。 阅读更多。
Home

条形码生成器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.78/5 (32投票s)

2016 年 1 月 10 日

CPOL

4分钟阅读

viewsIcon

84923

downloadIcon

20491

为您自己的业务、促销品或与朋友分享链接生成您自己的条形码。

引言

自己制作条形码很有用,尤其是如果您拥有自己的业务,或者您想推广您的业务或您自己,可以将条形码放在名片背面、商店橱窗里,还可以使用条形码制作独特的客户参考号等。

此应用程序使用所有 3 种类型的条形码

线性条形码(用于书籍、食品包装等)。

QR 条形码(用于名片、大型物品、礼品卡等)。

数据矩阵(我只在信件上看到过这种条形码)。

我需要指出的是,线性条形码和 QR 码支持透明背景,但数据矩阵不支持。

此外,用于线性条形码标签功能的字体按钮确实有效,但在选择包含的标签的字体之前需要填写选项,否则我们会收到错误。

开始编码吧!.

字符串 HelloWorld 的 QR 条形码结果。

 

 

使用代码

启动一个新的 Visual Studio 项目,并立即保存,以便 IDE 创建相关的目录。我喜欢将 DLL 库复制到应用程序目录,例如(项目名称/debug/bin),但这只是个人偏好。

将这些 dll 文件放置在合适的位置后,我们必须通过单击解决方案资源管理器,然后右键单击您的项目名称并单击“添加引用”按钮来为它们全部添加引用。找到并为所有 3 个 dll 文件添加引用。

我们首先导入库并声明一些变量。

Imports System                         
Imports System.Drawing                 
Imports ThoughtWorks.QRCode.Codec        ' this is the QRcode Library
Imports BarcodeLib.Barcode               ' this is the Linear library
Imports DataMatrix.net.DmtxImageEncoder  ' this is the Data Matrix library

    Dim QREncoder As ThoughtWorks.QRCode.Codec.QRCodeEncoder          ' QR code
    Dim LinearEncoder As BarcodeLib.Barcode                           ' LinearCdoe
    Dim DataEncoder As DataMatrix.net.DmtxImageEncoder                ' Data Matrix
    Dim DataEncodeOptions As DataMatrix.net.DmtxImageEncoderOptions   'set a variable to handle data
                                                                                      matrix options
    Dim Linearcode As Boolean  ' we are using these variables in an if statement
    Dim QRcode As Boolean      ' to determin what Barcode we are generating
    Dim Datacode As Boolean

    Dim QRimg As Image            ' Because the barcode result is an image we need to
    Dim QRbitmap As Bitmap        ' make a new image and bitmap in order to save the image
    Dim Linearimg As Image        '
    Dim Linearbitmap As Bitmap
    Dim Dataimg As Image
    Dim Databitmap As Bitmap
    Public barcodeImage As Bitmap ' we use this bitmap to assign the current barcode image to

    Dim DM_forecolour As Color    ' these variables are used to first fill the relevent comboboxes
    Dim DM_backcolour As Color    ' with the system colors and to use these colors to change the back
    Dim QR_foreColor As Color     ' and fore colors of the barcode image
    Dim QR_backColor As Color
    Dim L_foreColor As Color
    Dim L_backColor As Color
    Dim colorName As String       ' used to get all system colors

    Dim SelectedFont As Font      ' used to assign the selected font to the barcode label

    Dim X, Y As Integer           ' Declare 2 variables so we can make form_Viewer (form2)
    Dim buffer As Integer = 100   ' to the right size and add the buffer just to be sure we can see
                                  ' the entire Barcode

 

下一篇

注释行是为了让代码更易读。

Form1_Load

用于引用

Cbo_D_name 是数据矩阵组合框的组合框名称

Cbo_QR_name 是 QR 码组合框的组合框名称

Cbo_L_name 是线性码组合框的组合框名称

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '
        Rdo_QR.Checked = True            ' auto select QRcodes on startup
        QRcode = True                    ' 
        '---------------------------------------
        Cbo_D_Size.SelectedIndex = 12    ' auto set some combobox perameters on startup
        Cbo_D_Scheme.SelectedIndex = 0   '
        Cbo_D_Module.SelectedIndex = 1   ' 
        Cbo_D_Margin.SelectedIndex = 1   '
        '---------------------------------------
        Cbo_QR_Scale.SelectedIndex = 1
        Cbo_QR_Mode.SelectedIndex = 0
        Cbo_QR_Version.SelectedIndex = 5
        Cbo_QR_ErrorC.SelectedIndex = 0
        '---------------------------------------
        '
        ' add the fore and back colors to the  relevent comboboxes (Explained more below)
        '
        For Each Me.colorName In System.Enum.GetNames(GetType(System.Drawing.KnownColor))
            Cbo_D_fColor.Items.Add(Color.FromName(colorName))
            Cbo_D_bColor.Items.Add(Color.FromName(colorName))
            Cbo_QR_fColor.Items.Add(Color.FromName(colorName))
            Cbo_QR_bColor.Items.Add(Color.FromName(colorName))
            Cbo_L_bColor.Items.Add(Color.FromName(colorName))
            Cbo_L_fColor.Items.Add(Color.FromName(colorName))
            ' Label12.Text = Cbo_D_fColor.Items.Count  (Debug Line) just to see how many colors ther was
        Next
        '
    End Sub

 

下一篇

您可能希望在此处添加一个功能,即当最终用户单击其中一个单选按钮时,其他两个组框会被禁用

数据矩阵单选按钮

' DataMatrix RadioButton
    Private Sub Rdo_DataM_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rdo_DataM.CheckedChanged

        If Rdo_DataM.Checked = True Then
            Datacode = True
            Linearcode = False
            QRcode = False
            ' the feature mentioned above would be 
            ' groupboxQR.enabled = false
            ' groupboxLinear.enabled = false
            ' and apply the type of action on the other radio buttons
        End If

    End Sub

QR 单选按钮

 ' QR RadioButton
    Private Sub Rdo_QR_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rdo_QR.CheckedChanged

        If Rdo_QR.Checked = True Then
            QRcode = True
            Datacode = False
            Linearcode = False
        End If

    End Sub

线性单选按钮

 ' Linear RadioButton
    Private Sub Rdo_Linear_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rdo_Linear.CheckedChanged

        If Rdo_Linear.Checked = True Then
            Linearcode = True
            Datacode = False
            QRcode = False
        End If

    End Sub

下一篇

字体按钮代码

    'Label Font Button
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            ' without the TRY block the application would hang if all the options where not fill out
            '
            If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                LinearEncoder.LabelFont = FontDialog1.Font
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try
    End Sub

下一步 

为所有前景和背景组合框添加系统颜色

对于所有我们要为其添加系统颜色的组合框,我们需要设置组合框的绘制属性

设置为 OwnerDrawFixed,以便以编程方式添加颜色

数据矩阵颜色

    ' Handle the color selection for data matrix color
    '
    Private Sub Cbo_D_fColor_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Cbo_D_fColor.DrawItem
        If e.Index < 0 Then
            e.DrawBackground()
            e.DrawFocusRectangle()
            Exit Sub
        End If
        e.DrawBackground()
        e.DrawFocusRectangle()
        DM_forecolour = CType(Cbo_D_fColor.Items(e.Index), Color)
        e.Graphics.DrawString(DM_forecolour.Name, Cbo_D_fColor.Font, Brushes.Black, e.Bounds.Height + 5, ((e.Bounds.Height - Cbo_D_fColor.Font.Height) \ 2) + e.Bounds.Top)
    '
    End Sub
    '
    '
    '
    Private Sub Cbo_D_bColor_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Cbo_D_bColor.DrawItem
        '
        If e.Index < 0 Then
            e.DrawBackground()
            e.DrawFocusRectangle()
            Exit Sub
        End If
        e.DrawBackground()
        e.DrawFocusRectangle()
        DM_backcolour = CType(Cbo_D_bColor.Items(e.Index), Color)
        e.Graphics.DrawString(DM_backcolour.Name, Cbo_D_bColor.Font, Brushes.Black, e.Bounds.Height + 5, ((e.Bounds.Height - Cbo_D_bColor.Font.Height) \ 2) + e.Bounds.Top)
    '
    End Sub

我将包含添加其余代码的代码,但它真的很相似。

QR 颜色

 'Handle the color selection for QR color
    Private Sub Cbo_QR_fColor_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Cbo_QR_fColor.DrawItem
        If e.Index < 0 Then
            e.DrawBackground()
            e.DrawFocusRectangle()
            Exit Sub
        End If
        e.DrawBackground()
        e.DrawFocusRectangle()
        QR_foreColor = CType(Cbo_D_bColor.Items(e.Index), Color)

        e.Graphics.DrawString(QR_foreColor.Name, Cbo_D_bColor.Font, Brushes.Black, e.Bounds.Height + 5, ((e.Bounds.Height - Cbo_D_bColor.Font.Height) \ 2) + e.Bounds.Top)
    End Sub
    '
    Private Sub Cbo_QR_bColor_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Cbo_QR_bColor.DrawItem
        If e.Index < 0 Then
            e.DrawBackground()
            e.DrawFocusRectangle()
            Exit Sub
        End If
        e.DrawBackground()
        e.DrawFocusRectangle()
        QR_backColor = CType(Cbo_QR_bColor.Items(e.Index), Color)

        e.Graphics.DrawString(QR_backColor.Name, Cbo_QR_bColor.Font, Brushes.Black, e.Bounds.Height + 5, ((e.Bounds.Height - Cbo_QR_bColor.Font.Height) \ 2) + e.Bounds.Top)
    End Sub

线性颜色

'Handle the color selection for Linear color
    Private Sub Cbo_L_bColor_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Cbo_L_bColor.DrawItem
        If e.Index < 0 Then
            e.DrawBackground()
            e.DrawFocusRectangle()
            Exit Sub
        End If
        e.DrawBackground()
        e.DrawFocusRectangle()
        L_backColor = CType(Cbo_L_bColor.Items(e.Index), Color)

        e.Graphics.DrawString(L_backColor.Name, Cbo_L_bColor.Font, Brushes.Black, e.Bounds.Height + 5, ((e.Bounds.Height - Cbo_L_bColor.Font.Height) \ 2) + e.Bounds.Top)
    End Sub
    '
    Private Sub Cbo_L_fColor_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Cbo_L_fColor.DrawItem
        If e.Index < 0 Then
            e.DrawBackground()
            e.DrawFocusRectangle()
            Exit Sub
        End If
        e.DrawBackground()
        e.DrawFocusRectangle()
        L_foreColor = CType(Cbo_L_fColor.Items(e.Index), Color)

        e.Graphics.DrawString(L_foreColor.Name, Cbo_L_fColor.Font, Brushes.Black, e.Bounds.Height + 5, ((e.Bounds.Height - Cbo_L_fColor.Font.Height) \ 2) + e.Bounds.Top)
    End Sub

 

好的,现在是最有趣的部分,让我们编写生成子程序或函数,随你怎么说

这部分代码使用了大量的 if/else 语句,所以如果它看起来有点令人困惑,我表示歉意。

GenerateBarcode()

' Generate Function
    Public Sub GenerateBarcode(ByVal inputData As String)     ' a public sub with additional argument
                                                              ' inputData wich takes a string
        '
        '
        '
        If Linearcode = True Then    ' if the user has clicked the Liear Radio Button we
                                     ' generate a Linear Barcode
            '
            ' if the textbox has no text raise an error

            If Txt_InputData.Text.Length <= 0 Then
                MsgBox("You must Include the data to be Encoded.", MsgBoxStyle.OkOnly, "Data Error")
            Else

            LinearEncoder = New BarcodeLib.Barcode            'declare a new LinearEncoder
            LinearEncoder.AlternateLabel = Txt_label.Text     ' text to use as the label text
            TextBox1.Text = LinearEncoder.Country_Assigning_Manufacturer_Code ' most of the time this is            '                                                                 ' not used
            LinearEncoder.BackColor = L_backColor
            LinearEncoder.ForeColor = L_foreColor
            '
            '
            If CheckBox1.Checked = True Then
                LinearEncoder.IncludeLabel = True    ' include the label text in the encoded image
            End If
            '
            ' combobox for label positions
            If Cbo_L_LPosition.SelectedIndex = 0 Then
                LinearEncoder.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER
            ElseIf Cbo_L_LPosition.SelectedIndex = 1 Then
                LinearEncoder.LabelPosition = BarcodeLib.LabelPositions.BOTTOMLEFT
            ElseIf Cbo_L_LPosition.SelectedIndex = 2 Then
                LinearEncoder.LabelPosition = BarcodeLib.LabelPositions.BOTTOMRIGHT
            ElseIf Cbo_L_LPosition.SelectedIndex = 3 Then
                LinearEncoder.LabelPosition = BarcodeLib.LabelPositions.TOPCENTER
            ElseIf Cbo_L_LPosition.SelectedIndex = 4 Then
                LinearEncoder.LabelPosition = BarcodeLib.LabelPositions.TOPLEFT
            ElseIf Cbo_L_LPosition.SelectedIndex = 5 Then
                LinearEncoder.LabelPosition = BarcodeLib.LabelPositions.TOPRIGHT
            End If
            '
            '
            ' Linear barcode Encoding types
            '
            If Cbo_L_E_Type.SelectedIndex = 0 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.BOOKLAND
            ElseIf Cbo_L_E_Type.SelectedIndex = 1 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.Codabar
            ElseIf Cbo_L_E_Type.SelectedIndex = 2 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.CODE11
            ElseIf Cbo_L_E_Type.SelectedIndex = 3 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.CODE128
            ElseIf Cbo_L_E_Type.SelectedIndex = 4 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.CODE128A
            ElseIf Cbo_L_E_Type.SelectedIndex = 5 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.CODE128B
            ElseIf Cbo_L_E_Type.SelectedIndex = 6 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.CODE128C
            ElseIf Cbo_L_E_Type.SelectedIndex = 7 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.CODE39
            ElseIf Cbo_L_E_Type.SelectedIndex = 8 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.CODE39_Mod43
            ElseIf Cbo_L_E_Type.SelectedIndex = 9 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.CODE39Extended
            ElseIf Cbo_L_E_Type.SelectedIndex = 10 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.CODE93
            ElseIf Cbo_L_E_Type.SelectedIndex = 11 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.EAN13
            ElseIf Cbo_L_E_Type.SelectedIndex = 12 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.EAN8
            ElseIf Cbo_L_E_Type.SelectedIndex = 13 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.FIM
            ElseIf Cbo_L_E_Type.SelectedIndex = 14 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.Industrial2of5
            ElseIf Cbo_L_E_Type.SelectedIndex = 15 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.Interleaved2of5
            ElseIf Cbo_L_E_Type.SelectedIndex = 16 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.ISBN
            ElseIf Cbo_L_E_Type.SelectedIndex = 17 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.ITF14
            ElseIf Cbo_L_E_Type.SelectedIndex = 18 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.JAN13
            ElseIf Cbo_L_E_Type.SelectedIndex = 19 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.LOGMARS
            ElseIf Cbo_L_E_Type.SelectedIndex = 20 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.Modified_Plessey
            ElseIf Cbo_L_E_Type.SelectedIndex = 21 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.MSI_2Mod10
            ElseIf Cbo_L_E_Type.SelectedIndex = 22 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.MSI_Mod10
            ElseIf Cbo_L_E_Type.SelectedIndex = 23 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.MSI_Mod11
            ElseIf Cbo_L_E_Type.SelectedIndex = 24 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.MSI_Mod11_Mod10
            ElseIf Cbo_L_E_Type.SelectedIndex = 25 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.PHARMACODE
            ElseIf Cbo_L_E_Type.SelectedIndex = 26 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.PostNet
            ElseIf Cbo_L_E_Type.SelectedIndex = 27 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.Standard2of5
            ElseIf Cbo_L_E_Type.SelectedIndex = 28 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.TELEPEN
            ElseIf Cbo_L_E_Type.SelectedIndex = 29 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.UCC12
            ElseIf Cbo_L_E_Type.SelectedIndex = 30 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.UCC13
            ElseIf Cbo_L_E_Type.SelectedIndex = 31 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.UNSPECIFIED
            ElseIf Cbo_L_E_Type.SelectedIndex = 32 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_2DIGIT
            ElseIf Cbo_L_E_Type.SelectedIndex = 33 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_5DIGIT
            ElseIf Cbo_L_E_Type.SelectedIndex = 34 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.UPCA
            ElseIf Cbo_L_E_Type.SelectedIndex = 35 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.UPCE
            ElseIf Cbo_L_E_Type.SelectedIndex = 36 Then
                LinearEncoder.EncodedType = BarcodeLib.TYPE.USD8
            End If
            '
            '
            '
            Try   ' try to encode and report any errors
                '
                FontDialog1.FontMustExist = True           ' the font must actually exist on the system
                LinearEncoder.LabelFont = FontDialog1.Font ' set the label font
                '
                ' Encode the string into the barcode image
                '
                Linearimg = LinearEncoder.Encode(Cbo_L_E_Type.SelectedIndex, inputData)
                Linearbitmap = New Bitmap(Linearimg)
                Label9.Text = "Hashcode: " & LinearEncoder.GetHashCode
                '
                ' enable the label on form 2 to have the same hashcode
                Form_Viewer.Label1.Text = "Hashcode: " & LinearEncoder.GetHashCode
                barcodeImage = Linearbitmap
                '
                '
                X = Linearbitmap.Width               ' make Form_Viewer the right size
                Y = Linearbitmap.Height
                Form_Viewer.Width = X + buffer
                Form_Viewer.Height = Y + buffer
                '
            Catch ex As Exception
                MsgBox(ex.Message)  ' Catch any error and if yes then exit this sub
                Exit Sub
            End Try
            Form_Viewer.Show()      ' Show Form_Viewer (Form2)
          End If
          '
        End If
        '
        '
        ' QR Barcodes
        '
        If QRcode = True Then

            If Txt_InputData.Text.Length <= 0 Then
                MsgBox("You must Include the data to be Encoded.", MsgBoxStyle.OkOnly, "Data Error")
            Else

                QREncoder = New ThoughtWorks.QRCode.Codec.QRCodeEncoder       ' Declare a new QREncoder
                QREncoder.QRCodeForegroundColor = QR_foreColor
                QREncoder.QRCodeBackgroundColor = QR_backColor
                QREncoder.QRCodeScale = Cbo_QR_Scale.SelectedItem.ToString     ' QR scale
                QREncoder.QRCodeVersion = Cbo_QR_Version.SelectedItem.ToString ' QR version
                ' QR version is recomended to be a 6 this is because most barcode scanners
                ' are on mobile devices and version 6 is readable by most if not all scanners
                '
                ' QR Encode type
                '
                If Cbo_QR_Mode.SelectedIndex = 0 Then
                    QREncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC
                ElseIf Cbo_QR_Mode.SelectedIndex = 1 Then
                    QREncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE
                ElseIf Cbo_QR_Mode.SelectedIndex = 2 Then
                    QREncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC
                End If
                '
                ' QR Error Correction
                '
                If Cbo_QR_ErrorC.SelectedIndex = 0 Then
                    QREncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L
                ElseIf Cbo_QR_ErrorC.SelectedIndex = 1 Then
                    QREncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M
                ElseIf Cbo_QR_ErrorC.SelectedIndex = 2 Then
                    QREncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q
                ElseIf Cbo_QR_ErrorC.SelectedIndex = 2 Then
                    QREncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H
                End If
                '
                Try
                    QRimg = QREncoder.Encode(inputData)
                    QRbitmap = New Bitmap(QRimg)
                    barcodeImage = QRbitmap
                    Form_Viewer.Label1.Text = "Hashcode: " & QREncoder.GetHashCode
                    Label10.Text = "Hashcode: " & QREncoder.GetHashCode
                    '
                    '
                    X = QRbitmap.Width                ' make Form_Viewer the right size
                    Y = QRbitmap.Height
                    Form_Viewer.Width = X + buffer
                    Form_Viewer.Height = Y + buffer
                    '
                Catch ex As Exception
                    MsgBox(ex.Message)   ' Catch any errors and exit sub
                    Exit Sub
                End Try
                Form_Viewer.Show()       ' show Form_Viewer (Form2)
            End If
            '
        End If
            '
        '-------------------------------
        '
        ' Note that Transparency does Not work
        ' with Data Matrix Codes the resulting image will be black
        '
        ' Data Matrix Barcodes
        '          
        If Datacode = True Then

            If Txt_InputData.Text.Length <= 0 Then
                MsgBox("You must Include the data to be Encoded.", MsgBoxStyle.OkOnly, "Data Error")
            Else
                '
                ' Note for Data Matrix all the options are inside the TRY Block
                ' this is because the data matrix encoder not only takes in the string
                ' to be encoded but the options aswell
                '
                Try 'Encode
                    DataEncoder = New DataMatrix.net.DmtxImageEncoder
                    DataEncodeOptions = New DataMatrix.net.DmtxImageEncoderOptions
                    'Options-----------------------------------
                    '
                    ' size options
                    '
                    If Cbo_D_Size.SelectedIndex = 0 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol8x18
                    ElseIf Cbo_D_Size.SelectedIndex = 1 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol8x32
                    ElseIf Cbo_D_Size.SelectedIndex = 2 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol10x10
                    ElseIf Cbo_D_Size.SelectedIndex = 3 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol12x12
                    ElseIf Cbo_D_Size.SelectedIndex = 4 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol12x26
                    ElseIf Cbo_D_Size.SelectedIndex = 5 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol12x36
                    ElseIf Cbo_D_Size.SelectedIndex = 6 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol14x14
                    ElseIf Cbo_D_Size.SelectedIndex = 7 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol16x16
                    ElseIf Cbo_D_Size.SelectedIndex = 8 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol16x36
                    ElseIf Cbo_D_Size.SelectedIndex = 9 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol16x48
                    ElseIf Cbo_D_Size.SelectedIndex = 10 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol18x18
                    ElseIf Cbo_D_Size.SelectedIndex = 11 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol20x20
                    ElseIf Cbo_D_Size.SelectedIndex = 12 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol22x22
                    ElseIf Cbo_D_Size.SelectedIndex = 13 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol24x24
                    ElseIf Cbo_D_Size.SelectedIndex = 14 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol26x26
                    ElseIf Cbo_D_Size.SelectedIndex = 15 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol32x32
                    ElseIf Cbo_D_Size.SelectedIndex = 16 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol36x36
                    ElseIf Cbo_D_Size.SelectedIndex = 17 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol40x40
                    ElseIf Cbo_D_Size.SelectedIndex = 18 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol44x44
                    ElseIf Cbo_D_Size.SelectedIndex = 19 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol48x48
                    ElseIf Cbo_D_Size.SelectedIndex = 20 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol52x52
                    ElseIf Cbo_D_Size.SelectedIndex = 21 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol64x64
                    ElseIf Cbo_D_Size.SelectedIndex = 22 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol72x72
                    ElseIf Cbo_D_Size.SelectedIndex = 23 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol80x80
                    ElseIf Cbo_D_Size.SelectedIndex = 24 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol88x88
                    ElseIf Cbo_D_Size.SelectedIndex = 25 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol96x96
                    ElseIf Cbo_D_Size.SelectedIndex = 26 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbolRectAuto
                    ElseIf Cbo_D_Size.SelectedIndex = 27 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbolShapeAuto
                    ElseIf Cbo_D_Size.SelectedIndex = 28 Then
                        DataEncodeOptions.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbolSquareAuto
                    End If
                    '
                    '
                    ' scheme options
                    '
                    If Cbo_D_Scheme.SelectedIndex = 0 Then
                        DataEncodeOptions.Scheme = DataMatrix.net.DmtxScheme.DmtxSchemeAscii
                    ElseIf Cbo_D_Size.SelectedIndex = 1 Then
                        DataEncodeOptions.Scheme = DataMatrix.net.DmtxScheme.DmtxSchemeAsciiGS1
                    ElseIf Cbo_D_Size.SelectedIndex = 2 Then
                        DataEncodeOptions.Scheme = DataMatrix.net.DmtxScheme.DmtxSchemeAutoBest
                    ElseIf Cbo_D_Size.SelectedIndex = 3 Then
                        DataEncodeOptions.Scheme = DataMatrix.net.DmtxScheme.DmtxSchemeAutoFast
                    ElseIf Cbo_D_Size.SelectedIndex = 4 Then
                        DataEncodeOptions.Scheme = DataMatrix.net.DmtxScheme.DmtxSchemeBase256
                    ElseIf Cbo_D_Size.SelectedIndex = 5 Then
                        DataEncodeOptions.Scheme = DataMatrix.net.DmtxScheme.DmtxSchemeC40
                    ElseIf Cbo_D_Size.SelectedIndex = 6 Then
                        DataEncodeOptions.Scheme = DataMatrix.net.DmtxScheme.DmtxSchemeEdifact
                    ElseIf Cbo_D_Size.SelectedIndex = 7 Then
                        DataEncodeOptions.Scheme = DataMatrix.net.DmtxScheme.DmtxSchemeText
                    ElseIf Cbo_D_Size.SelectedIndex = 8 Then
                        DataEncodeOptions.Scheme = DataMatrix.net.DmtxScheme.DmtxSchemeX12
                    End If
                    '
                    ' module size options
                    '
                    If Cbo_D_Module.SelectedIndex = 0 Then
                        DataEncodeOptions.ModuleSize = 1
                    ElseIf Cbo_D_Module.SelectedIndex = 1 Then
                        DataEncodeOptions.ModuleSize = 2
                    ElseIf Cbo_D_Module.SelectedIndex = 2 Then
                        DataEncodeOptions.ModuleSize = 3
                    ElseIf Cbo_D_Module.SelectedIndex = 3 Then
                        DataEncodeOptions.ModuleSize = 4
                    ElseIf Cbo_D_Module.SelectedIndex = 4 Then
                        DataEncodeOptions.ModuleSize = 5
                    ElseIf Cbo_D_Module.SelectedIndex = 5 Then
                        DataEncodeOptions.ModuleSize = 6
                    ElseIf Cbo_D_Module.SelectedIndex = 6 Then
                        DataEncodeOptions.ModuleSize = 7
                    ElseIf Cbo_D_Module.SelectedIndex = 7 Then
                        DataEncodeOptions.ModuleSize = 8
                    ElseIf Cbo_D_Module.SelectedIndex = 8 Then
                        DataEncodeOptions.ModuleSize = 9
                    ElseIf Cbo_D_Module.SelectedIndex = 9 Then
                        DataEncodeOptions.ModuleSize = 10
                    End If
                    '
                    ' margin size options
                    '
                    If Cbo_D_Margin.SelectedIndex = 0 Then
                        DataEncodeOptions.MarginSize = 1
                    ElseIf Cbo_D_Margin.SelectedIndex = 1 Then
                        DataEncodeOptions.MarginSize = 2
                    ElseIf Cbo_D_Margin.SelectedIndex = 2 Then
                        DataEncodeOptions.MarginSize = 3
                    ElseIf Cbo_D_Margin.SelectedIndex = 3 Then
                        DataEncodeOptions.MarginSize = 4
                    ElseIf Cbo_D_Margin.SelectedIndex = 4 Then
                        DataEncodeOptions.MarginSize = 5
                    End If
                    '
                    DataEncodeOptions.ForeColor = DM_forecolour
                    DataEncodeOptions.BackColor = DM_backcolour
                    '---------------------------
                    '
                    ' DataEncoder takes the inputdata string AND the DataEncode Options
                    '
                    Dataimg = DataEncoder.EncodeImage(inputData, DataEncodeOptions)
                    Databitmap = New Bitmap(Dataimg)
                    barcodeImage = Databitmap
                    Label12.Text = "Hashcode: " & DataEncoder.GetHashCode
                    Form_Viewer.Label1.Text = "Hashcode: " & DataEncoder.GetHashCode
                    '                   
                    '
                    '
                    X = Databitmap.Width              ' when Form_Viewer opens make the window
                    Y = Databitmap.Height             ' the right size
                    Form_Viewer.Width = X + buffer
                    Form_Viewer.Height = Y + buffer
                    '
                Catch ex As Exception
                    MsgBox(ex.Message)
                    Exit Sub
                End Try
                Form_Viewer.Show()
                '
            End If
            '
        End If
        '
    End Sub

 

请确保用正确的项目填充相应的组合框,例如

Cbo_D_Scheme 将需要将以下项目添加到其集合中

Ascii
AsciiGS1
AutoBest
AutoFast
Base256
C40
Edifact
文本
X12

等等,等等。

下一篇

Form_Viewer

Form2 的设置很简单,我们只需要

一个 Panel                  如果 PictureBox 比窗口大,则使用此控件,因为 Panel 可以滚动

                              PictureBox 不能。

一个 Picturebox           用于显示生成的条形码图像

一个 Combobox         用于选择合适的保存类型

一个 Label                   用于显示哈希码

一个 Button                 用于保存图像

一个 StatusStrip         用于放置组合框和按钮,而不使用状态栏中的任何项目

(检查上图)

 

Form_Viewer 代码

    '    
    Dim CodeBitmap As Bitmap        ' declare a new bitmap
    Dim SFD As SaveFileDialog       ' delcare a save file dialog box
    '---------------------------------------
    '
    '
    Private Sub Form_Viewer_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        '
        ' Do this when this form closes
        '
        Form1.Label9.Text = ""
        Form1.Label10.Text = ""
        Form1.Label12.Text = ""
    End Sub
    '
    '
    '
    Private Sub Form_Viewer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '
        ' make a new savefile dialog
        SFD = New SaveFileDialog
        '
        ' savefiledialog filter (save types)
        '
       SFD.Filter = "Bmp (*.bmp) |*.bmp | Jpeg (*.jpg)|*.jpg | PNG (*.png)|*.png | TIFF (*.tiff)|*.tiff"
        '
        '
        ComboBox1.SelectedIndex = 2      ' auto select a save type on load
        SFD.AddExtension = True          ' will this savefile dialog add an extension to the filename
        '
        PicBox1.BackgroundImage = Form1.barcodeImage
        CodeBitmap = New Bitmap(Form1.barcodeImage)
        '
    End Sub
    '
    '
    ' Save Button
    '
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '
        '
        If ComboBox1.SelectedIndex = 0 Then
            SFD.FilterIndex = 1
        ElseIf ComboBox1.SelectedIndex = 1 Then
            SFD.FilterIndex = 2
        ElseIf ComboBox1.SelectedIndex = 2 Then
            SFD.FilterIndex = 3
        ElseIf ComboBox1.SelectedIndex = 3 Then
            SFD.FilterIndex = 4
        ElseIf ComboBox1.SelectedIndex = 4 Then
            SFD.FilterIndex = 5
        End If
        '
        If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then   ' Save the image to your choice of
            CodeBitmap.Save(SFD.FileName)                        ' location
        End If
        '
    End Sub

 

结束

dll 文件在一个单独的 zip/rar 文件中,如果您只想下载这些。

当然,您也可以下载整个项目。

未来的进一步功能

如果您决定将此应用程序用于销售,那么添加一个随机字符串生成器可能会很有用,并且

根据您的客户生成字符串,并从唯一字符串生成条形码图像

或始终唯一的 GUID 号。

 

注释

如果您看到错误,或者想发布新功能或只是普通反馈,请留下评论。

 

 

 

 

© . All rights reserved.