- Start Visual Studio .NET.
- Start with the project that you created in Reading Bar Codes Tutorial
- Drag and drop a button on Form1. Change the following properties: Property Value: Name: btnWriteBarCode Text: Write BarCode
- Add the following code for the btnWriteBarCode control’s click procedure:
Visual Basic
Private Sub btnWriteBarCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteBarCode.Click Dim barcodeData As String() = New String(0){} barcodeData(0) = "012345678901" Dim barColor As BarcodeColor = New BarcodeColor() Dim barData As BarcodeData = New BarcodeData() barData.Unit = BarcodeUnit.ScanlinesPerPixels barData.Location = New LeadRect(0, 0, 500, 250) barData.Data = BarcodeData.ConvertFromStringArray(barcodeData) barData.SearchType = BarcodeSearchTypeFlags.Barcode1dCode3Of9 Dim bar1d As Barcode1d = New Barcode1d() bar1d.ErrorCheck = True bar1d.OutShowText = True Try _barEngine.Write(_image, barData, barColor, BarcodeWriteFlags.None, bar1d, Nothing, Nothing, Nothing, LeadRect.Empty) Dim fName As String = "C:\Users\Public\Documents\LEADTOOLS Images\barcode_modified.tif" _codecs.Save(_image, fName, RasterImageFormat.CcittGroup4, 0) MessageBox.Show(Me, String.Format("The barcode has been written and saved to {0}", fName)) Catch ex As Exception MessageBox.Show(Me, ex.Message) End Try End Sub
C#
private void btnWriteBarCode_Click(object sender, EventArgs e) { string[] barcodeData = new string[1]; barcodeData[0] = "012345678901"; BarcodeColor barColor = new BarcodeColor(); BarcodeData barData = new BarcodeData(); barData.Unit = BarcodeUnit.ScanlinesPerPixels; barData.Location = new LeadRect(0, 0, 500, 250); barData.Data = BarcodeData.ConvertFromStringArray(barcodeData); barData.SearchType = BarcodeSearchTypeFlags.Barcode1dCode3Of9; Barcode1d bar1d = new Barcode1d(); bar1d.ErrorCheck = true; bar1d.OutShowText = true; try { _barEngine.Write(_image, barData, barColor, BarcodeWriteFlags.None, bar1d, null, null, null, LeadRect.Empty); string fName = @"C:\Users\Public\Documents\LEADTOOLS Images\barcode_modified.tif"; _codecs.Save(_image, fName, RasterImageFormat.CcittGroup4, 0); MessageBox.Show(this, String.Format("The barcode has been written and saved to {0}", fName)); } catch (Exception ex) { MessageBox.Show(this, ex.Message); } }
- Build, and Run the program to test it. Click "Load Image" to load the image and "Write Barcode" to write a 1D barcode to the image and save it