Writing Barcodes

  1. Start Visual Studio.

  2. Start with the project that you created in Reading Barcodes Tutorial

  3. Drag and drop a new button in Form1. Change the following properties:

    Property Value
    Name writeBarcodeButton
    Text Write barcode
  4. Add the following code to writeBarcodeButton click procedure:

    VB
    Private Sub writeBarcodeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles writeBarcodeButton.Click 
       If theImage Is Nothing Then 
          Return 
       End If 
                     
       ' Create a UPC A barcode 
       Dim data As New BarcodeData() 
       data.Symbology = BarcodeSymbology.UPCA 
       data.Value = "01234567890" 
       data.Bounds = New LeadRect(10, 10, 600, 200) 
                     
       ' Setup the options to enable error checking and show the text on the bottom of the barcode 
       Dim options As New OneDBarcodeWriteOptions() 
       options.EnableErrorCheck = True 
       options.TextPosition = BarcodeOutputTextPosition.Default 
                     
       Try 
          ' Write the barcode 
          barcodeEngineInstance.Writer.WriteBarcode(theImage, data, options) 
                     
          ' Save the image 
          Dim dir As String = System.IO.Path.GetDirectoryName(imageFileName) 
          Dim name As String = System.IO.Path.GetFileNameWithoutExtension(imageFileName) 
           Dim saveFileName as String = System.IO.Path.Combine(dir, name + "_WriteBarcode.tif") 
                     
          Using codecs As New RasterCodecs() 
             codecs.Save(theImage, saveFileName, RasterImageFormat.Tif, theImage.BitsPerPixel) 
          End Using 
                     
          MessageBox.Show(String.Format("The barcode has been written and saved to {0}", saveFileName)) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    C#
    private void writeBarcodeButton_Click(object sender, EventArgs e) 
    { 
       if(theImage == null) 
       { 
          return; 
       } 
                     
       // Create a UPC A barcode 
       BarcodeData data = new BarcodeData(); 
       data.Symbology = BarcodeSymbology.UPCA; 
       data.Value = "01234567890"; 
       data.Bounds = new LeadRect(10, 10, 600, 200); 
                     
       // Setup the options to enable error checking and show the text on the bottom of the barcode 
       OneDBarcodeWriteOptions options = new OneDBarcodeWriteOptions(); 
       options.EnableErrorCheck = true; 
       options.TextPosition = BarcodeOutputTextPosition.Default; 
                     
       try 
       { 
          // Write the barcode 
          barcodeEngineInstance.Writer.WriteBarcode(theImage, data, options); 
                     
          // Save the image 
          string dir = System.IO.Path.GetDirectoryName(imageFileName); 
          string name = System.IO.Path.GetFileNameWithoutExtension(imageFileName); 
          string saveFileName = System.IO.Path.Combine(dir, name + "_WriteBarcode.tif"); 
                     
          using(RasterCodecs codecs = new RasterCodecs()) 
          { 
             codecs.Save(theImage, saveFileName, RasterImageFormat.Tif, theImage.BitsPerPixel); 
          } 
                     
          MessageBox.Show(string.Format("The barcode has been written and saved to {0}", saveFileName)); 
       } 
       catch(Exception ex) 
       { 
          MessageBox.Show(ex.Message); 
       } 
    } 

  5. Build, and Run the program to test it. Click "Load Image" to load the image and "Write Barcode" to write a 1D UPC A barcode to the image and save it.
Help Version 20.0.2020.4.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Imaging, Medical, and Document