Introduction
It is useful to make you own barcodes especially if you own your own business or your wanting to promote what you do or who you are, place a barcode on the back of your business card, in a shop window, use barcodes for making unique customer reference numbers etc.
This application uses all 3 types of barcode:
Linear Barcode (used on books, food packaging etc ).
QR Barcodes (used on business cards on large items, giftcards etc ).
Data Matrix (i have only seen this type of barcode on letters ) .
I would like to point out now that Linear and QR Codes support Transparent backgrounds but the Data Matrix dosen't.
And also the font button for the label feture on the Linear Barcodes does work but the options need to be filled in before selecting a font for the included label otherwise we will get an error.
Let's Get Coding!.
The QR Barcode result of the string HelloWorld.
Using the code
Start a new Visual Studio Project and save straight away so the IDE Makes the relevent directories, I like to copy the DLL Librarys into the application directory eg. (ProjectName/debug/bin) but it is personal preference.
Once these dll's have been placed in a suitable location we must add references for all of them by clicking on the Solution Explorer, then right mouse click on your project name and click 'Add Reference' button, Locate and add a reference to all 3 dll files.
We start by Importing the Librarys and declaring some Variables.
Imports System
Imports System.Drawing
Imports ThoughtWorks.QRCode.Codec Imports BarcodeLib.Barcode Imports DataMatrix.net.DmtxImageEncoder
Dim QREncoder As ThoughtWorks.QRCode.Codec.QRCodeEncoder Dim LinearEncoder As BarcodeLib.Barcode Dim DataEncoder As DataMatrix.net.DmtxImageEncoder Dim DataEncodeOptions As DataMatrix.net.DmtxImageEncoderOptions matrix options
Dim Linearcode As Boolean Dim QRcode As Boolean Dim Datacode As Boolean
Dim QRimg As Image Dim QRbitmap As Bitmap Dim Linearimg As Image Dim Linearbitmap As Bitmap
Dim Dataimg As Image
Dim Databitmap As Bitmap
Public barcodeImage As Bitmap
Dim DM_forecolour As Color Dim DM_backcolour As Color Dim QR_foreColor As Color Dim QR_backColor As Color
Dim L_foreColor As Color
Dim L_backColor As Color
Dim colorName As String
Dim SelectedFont As Font
Dim X, Y As Integer Dim buffer As Integer = 100
Next
There are comment lines just to try and make the code more readable.
Form1_Load
for referencing purposes:
Cbo_D_name is the combobox Name for the Data Matrix comboboxes
Cbo_QR_name is combobox the Name for the QRcodes comboboxes
Cbo_L_name is the combobox Name for the Linear codes comboboxes
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Rdo_QR.Checked = True QRcode = True Cbo_D_Size.SelectedIndex = 12 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
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))
Next
End Sub
Next
A feature you may wish to add here is when the end user click on one of the radio buttons the other two groupboxes are disabled
DataMatrix Radio selection button
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
End If
End Sub
QR Radio selection button
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 Radio selection button
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
Next
Font Button Code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
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
Next
Adding the system colors the all of the Fore and Background comboboxes
for all of the comboboxes we use to add system colors to we need to set the combobox draw property
to OwnerDrawFixed in order to programatically add the colors
Data Matrix Colors
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
I will include the code for adding the rest but its really the same code.
QR Colors
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
Linear Colors
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
Ok so heres the fun part let's code the generate sub or function however you wish to say it
this part of the code uses a lot of if,else statements so i apollogise if it may look a little confusing.
GenerateBarcode()
Public Sub GenerateBarcode(ByVal inputData As String) If Linearcode = True Then
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 LinearEncoder.AlternateLabel = Txt_label.Text TextBox1.Text = LinearEncoder.Country_Assigning_Manufacturer_Code LinearEncoder.BackColor = L_backColor
LinearEncoder.ForeColor = L_foreColor
If CheckBox1.Checked = True Then
LinearEncoder.IncludeLabel = True End If
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
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 FontDialog1.FontMustExist = True LinearEncoder.LabelFont = FontDialog1.Font Linearimg = LinearEncoder.Encode(Cbo_L_E_Type.SelectedIndex, inputData)
Linearbitmap = New Bitmap(Linearimg)
Label9.Text = "Hashcode: " & LinearEncoder.GetHashCode
Form_Viewer.Label1.Text = "Hashcode: " & LinearEncoder.GetHashCode
barcodeImage = Linearbitmap
X = Linearbitmap.Width Y = Linearbitmap.Height
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
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 QREncoder.QRCodeForegroundColor = QR_foreColor
QREncoder.QRCodeBackgroundColor = QR_backColor
QREncoder.QRCodeScale = Cbo_QR_Scale.SelectedItem.ToString QREncoder.QRCodeVersion = Cbo_QR_Version.SelectedItem.ToString 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
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 Y = QRbitmap.Height
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
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
Try DataEncoder = New DataMatrix.net.DmtxImageEncoder
DataEncodeOptions = New DataMatrix.net.DmtxImageEncoderOptions
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
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
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
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
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 Y = Databitmap.Height 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
Be sure to fill the appropriate comboboxes with the right items for example:
Cbo_D_Scheme will need these items added to it's collection
Ascii
AsciiGS1
AutoBest
AutoFast
Base256
C40
Edifact
Text
X12
Etc, Etc.
Next
Form_Viewer
The setup for form2 is quite simple all we need is:
A Panel this is used incase the picturebox ends up larger then the window as a panel can be scrolled
a picturebox cannot.
A Picturebox for displaying the resulting Barcode image
A Combobox for choosing an appropriate save type
A Label for displaying the hashcode
A Button to save the image
A StatusStrip for somewhere to place the combobox and button where not using any items from the status strip
(check the image above)
Form_Viewer Code
Dim CodeBitmap As Bitmap Dim SFD As SaveFileDialog Private Sub Form_Viewer_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
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
SFD = New SaveFileDialog
SFD.Filter = "Bmp (*.bmp) |*.bmp | Jpeg (*.jpg)|*.jpg | PNG (*.png)|*.png | TIFF (*.tiff)|*.tiff"
ComboBox1.SelectedIndex = 2 SFD.AddExtension = True PicBox1.BackgroundImage = Form1.barcodeImage
CodeBitmap = New Bitmap(Form1.barcodeImage)
End Sub
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 CodeBitmap.Save(SFD.FileName) End If
End Sub
Finish
The dll files are in a seperate zip/rar file if you wish to download those only.
You can of course download the whole project if need be.
Further Features for the Future
If you decide to use this application for sales it might be worth adding a random string generator and
generate a string based on your customer and generate the barcode image from the unique string
or GUID number which is always unique.
Notes:
Please leave comments if you see something wrong or even would like to post a new feature or just general feedback.