Take the following steps to start a project:
- Start Visual Studio .NET.
- Choose File->New->Project… from the menu.
- In the New Project dialog box, Choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
- Type the project name as "GetFastTwainScan" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.
- In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference…" from the context menu. In the Add Reference dialog box, select the ".NET" tab and browse to Leadtools For .NET
"LEAD Technologies\LEADTOOLS 16.5\Bin\DotNet\Win32" folder and select the following DLLs:
- Leadtools.dll
- Leadtools.Barcode.dll
- Leadtools.Codecs.dll
-
Switch to Form1 code
view (right-click Form1 in the solution explorer then select
View Code ) and add the following lines at the beginning of the
file:
[Visual Basic]
[C#]Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.Barcode
using Leadtools; using Leadtools.Codecs; using Leadtools.Barcode;
- Drag and drop two buttons in Form1. Change the following properties: Property Value: Name: btnLoadImage Text: Load Image Name: btnReadBarCode Text: Read Bar Code
- Add an event handler to the Form1 Load event and add the following code
Visual Basic
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Note that this is a sample key, which will not work in your toolkit RasterSupport.Unlock(Leadtools.RasterSupportType.Barcodes1D, "TestKey") BarcodeEngine.Startup(BarcodeMajorTypeFlags.Barcodes1d) RasterCodecs.Startup() _barEngine = New BarcodeEngine() _codecs = New RasterCodecs() End Sub
C#
private void Form1_Load(object sender, EventArgs e) { // Note that this is a sample key, which will not work in your toolkit RasterSupport.Unlock(Leadtools.RasterSupportType.Barcodes1D, "TestKey"); BarcodeEngine.Startup(BarcodeMajorTypeFlags.Barcodes1d); RasterCodecs.Startup(); _barEngine = new BarcodeEngine(); _codecs = new RasterCodecs(); }
- Add an event handler to the Form1 FormClosing event and add the following code
Visual Basic
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing BarcodeEngine.Shutdown() End Sub
C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { BarcodeEngine.Shutdown(); }
- Add the following private variables to Form1 class:
Visual Basic
Dim _image As RasterImage Dim _barEngine As BarcodeEngine Dim _codecs As RasterCodecs Dim _readBarData As RasterCollection(Of BarcodeData)
C#
RasterImage _image; BarcodeEngine _barEngine; RasterCodecs _codecs; RasterCollection<BarcodeData> _readBarData;
- Add the following code for the btnLoadImage control’s click procedure:
Visual Basic
Private Sub btnLoadImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadImage.Click _image = _codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\barcode1.tif") End Sub
C#
private void btnLoadImage_Click(object sender, EventArgs e) { _image = _codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\barcode1.tif"); }
- Add the following code for the btnReadBarCode control’s click procedure:
Visual Basic
Private Sub btnReadBarCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadBarCode.Click Dim searchRect As Rectangle = New Rectangle(0, 0, 0, 0) Dim bar1d As Barcode1d = New Barcode1d() bar1d.ErrorCheck = True bar1d.Granularity = 9 bar1d.Direction = BarcodeDirectionFlags.LeftToRight Dim barRPdf As BarcodeReadPdf = New BarcodeReadPdf() barRPdf.Direction = BarcodeDirectionFlags.LeftToRight Dim barColor As BarcodeColor = New BarcodeColor() barColor.BarColor = Color.Black barColor.SpaceColor = Color.White Try ' The Read method will search for all linear barcodes in the whole bitmap _readBarData = _barEngine.Read(_image, _ searchRect, _ BarcodeSearchTypeFlags.Barcode1dReadAnyType, _ BarcodeUnit.ScanlinesPerPixels, _ BarcodeReadFlags.None, _ 0, _ bar1d, _ barRPdf, _ barColor) Dim count As Integer = _readBarData.Count Dim i As Integer For i = 0 To count - 1 Dim data As BarcodeData = _readBarData(i) Dim barDataMsg As String = String.Format("Barcode Data = {0}\nType = {1}\nLeft = {2}\nTop = {3}\nRight = {4}\nBottom = {5}\n", _ BarcodeData.ConvertToStringArray(data.Data), _ data.SearchType, _ data.Location.Left, _ data.Location.Top, _ data.Location.Right, _ data.Location.Bottom) MessageBox.Show(Me, barDataMsg) Next Catch ex As Exception MessageBox.Show(Me, ex.Message) End Try End Sub
C#
private void btnReadBarCode_Click(object sender, EventArgs e) { Rectangle searchRect = new Rectangle(0, 0, 0, 0); Barcode1d bar1d = new Barcode1d(); bar1d.ErrorCheck = true; bar1d.Granularity = 9; bar1d.Direction = BarcodeDirectionFlags.LeftToRight; BarcodeReadPdf barRPdf = new BarcodeReadPdf(); barRPdf.Direction = BarcodeDirectionFlags.LeftToRight; BarcodeColor barColor = new BarcodeColor(); barColor.BarColor = Color.Black; barColor.SpaceColor = Color.White; try { // The Read method will search for all linear barcodes in the whole bitmap _readBarData = _barEngine.Read(_image, searchRect, BarcodeSearchTypeFlags.Barcode1dReadAnyType, BarcodeUnit.ScanlinesPerPixels, BarcodeReadFlags.None, 0, bar1d, barRPdf, barColor); int count = _readBarData.Count; for (int i=0; i<count; i++) { BarcodeData data = _readBarData[i]; string barDataMsg = string.Format("Barcode Data = {0}\nType = {1}\nLeft = {2}\nTop = {3}\nRight = {4}\nBottom = {5}\n", BarcodeData.ConvertToStringArray(data.Data), data.SearchType, data.Location.Left, data.Location.Top, data.Location.Right, data.Location.Bottom); MessageBox.Show(this, barDataMsg); } } catch (Exception ex) { MessageBox.Show(this, ex.Message); } }