Start Visual Studio .NET.
Start with the project that you created in Reading Barcodes Tutorial
Drag and drop a new button in Form1. Change the following properties:
Property | Value |
---|---|
Name | writeBarcodeButton |
Text | Write barcode |
Add the following code to writeBarcodeButton click procedure:
[Visual Basic]
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 LogicalRectangle(10, 10, 600, 200, LogicalUnit.Pixel) ' 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 = OneDBarcodeTextPosition.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 LogicalRectangle(10, 10, 600, 200, LogicalUnit.Pixel); // 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 = OneDBarcodeTextPosition.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); } }
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.