Take the following steps to create and run a program that acquires an image from a TWAIN source.
Start Visual Studio.
Choose File -> New -> Project… from the menu.
In the New Project dialog box, choose "Windows Classic Desktop" from either the "Visual C#" or "Visual Basic" node in the Templates List. Then choose Windows Forms App (.NET Framework).
Type the project name as "Acquiring an Image" in the project Name field. If desired, type a new location for your project or select a directory using the Browse button. Click 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, browse to the LEADTOOLS bin folder (<LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32) and select the following DLLs:
Click the Select button and then click the OK button to add the above DLLs to the application.
Make sure Form1 is in design view. Go to the toolbox (View -> Toolbox) and drag and drop an instance of ImageViewer to the form. If you do not have ImageViewer in your toolbox, select Tools -> Choose Toolbox Items from the menu. Click Browse and then select Leadtools.Controls.WinForms.DLL from "<LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32" and then click Open and then click OK.
Go to the toolbox (View -> Toolbox) and drag and drop 3 instances of the RadioButton control to the top of the form and set the following properties for them:
| Text | Name | Checked |
| Native | radioNative | False |
| Memory | radioMemory | False |
| File | radioFile | False |
| Text | Name |
|---|---|
| Acquire | buttonAcquire |
| Select Source | buttonSelectSource |
| Save Template File | buttonSaveTemplateFile |
| Load Template File | buttonLoadTemplateFile |
Switch to the code view of Form1 (in the Solution Explorer, right-click Form1 then select View Code) and add the following lines at the beginning of the file:
Imports LeadtoolsImports Leadtools.Twain
using Leadtools;using Leadtools.Twain;
Declare the following private variable:
Private WithEvents _twnSession As TwainSession
private TwainSession _twnSession;
Add an event handler to the Form1 Load event and code it as follows:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadTryDim myLicenseFile As String = "C:\LEADTOOLS 19\Common\License\leadtools.lic"Dim myDeveloperKey As String = System.IO.File.ReadAllText("C:\LEADTOOLS 19\Common\License\leadtools.lic.key")RasterSupport.SetLicense(myLicenseFile, myDeveloperKey)_twnSession = new TwainSession_twnSession.Startup(Me.Handle, "manufacturer", "productFamily", "version", "application", TwainStartupFlags.None)Catch ex As ExceptionMessageBox.Show(ex.Message)End TryEnd Sub
private void Form1_Load(object sender, System.EventArgs e){try{const string myLicenseFile = @"C:\LEADTOOLS 19\Common\License\leadtools.lic";string myDeveloperKey = System.IO.File.ReadAllText(@"C:\LEADTOOLS 19\Common\License\leadtools.lic.key");RasterSupport.SetLicense(myLicenseFile, myDeveloperKey);_twnSession = new TwainSession();_twnSession.Startup(this.Handle, "manufacturer", "productFamily", "version", "application", TwainStartupFlags.None);}catch (Exception ex){MessageBox.Show(this, ex.Message);}}
Add an event handler to the Form1 Closing event and code it as follows:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosingTry_twnSession.Shutdown()Catch ex As ExceptionMessageBox.Show(ex.Message)End TryEnd Sub
private void Form1_FormClosing(object sender, FormClosingEventArgs e){try{_twnSession.Shutdown();}catch (Exception ex){MessageBox.Show(this, ex.Message);}}
Add an event handler to the _twnSession AcquirePage event and code it as follows:
Private Sub _twnSession_AcquirePage(ByVal sender As Object, ByVal e As TwainAcquirePageEventArgs) Handles _twnSession.AcquirePageRasterImageViewer1.Image = e.ImageEnd Sub
private void _twnSession_AcquirePage(object sender, TwainAcquirePageEventArgs e){rasterImageViewer1.Image = e.Image;}
Add an event handler to the buttonAcquire Click event and code it as follows:
Private Sub buttonAcquire_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonAcquire.ClickTry_twnSession.Acquire(TwainUserInterfaceFlags.Show)Catch ex As ExceptionMessageBox.Show(ex.Message)End TryEnd Sub
private void buttonAcquire_Click(object sender, System.EventArgs e){try{_twnSession.AcquirePage += new EventHandler<TwainAcquirePageEventArgs>(_twnSession_AcquirePage);_twnSession.Acquire(TwainUserInterfaceFlags.Show);}catch (Exception ex){MessageBox.Show(this, ex.Message);}}
Add an event handler to the buttonSelectSource Click event and code it as follows:
Private Sub buttonSelectSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonSelectSource.ClickTry_twnSession.SelectSource(String.Empty)Catch ex As ExceptionMessageBox.Show(ex.Message)End TryEnd Sub
private void buttonSelectSource_Click(object sender, System.EventArgs e){try{_twnSession.SelectSource(string.Empty);}catch (Exception ex){MessageBox.Show(this, ex.Message);}}
Add an event handler to the buttonSaveTemplateFile Click event and code it as follows:
Private Sub buttonSaveTemplateFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonSaveTemplateFile.ClickTry_twnSession.SaveTemplateFile("c:\test.ltt")Catch ex As ExceptionMessageBox.Show(ex.Message)End TryEnd Sub
private void buttonSaveTemplateFile_Click(object sender, System.EventArgs e){try{_twnSession.SaveTemplateFile(@"c:\test.ltt");}catch (Exception ex){MessageBox.Show(this, ex.Message);}}
Add an event handler to the buttonLoadTemplateFile Click event and code it as follows:
Private Sub buttonLoadTemplateFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonLoadTemplateFile.ClickTry_twnSession.LoadTemplateFile("c:\test.ltt")Catch ex As ExceptionMessageBox.Show(ex.Message)End TryEnd Sub
private void buttonLoadTemplateFile_Click(object sender, System.EventArgs e){try{_twnSession.LoadTemplateFile(@"c:\test.ltt");}catch (Exception ex){MessageBox.Show(this, ex.Message);}}
Add an event handler to the radioNative CheckedChanged event and code it as follows:
Private Sub radioNative_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radioNative.CheckedChangedTryDim capability As New TwainCapabilitycapability.Information.ContainerType = TwainContainerType.OneValuecapability.Information.Type = TwainCapabilityType.ImageTransferMechanismcapability.OneValueCapability.ItemType = TwainItemType.Uint16capability.OneValueCapability.Value = CUShort(TwainTransferMechanism.Native)_twnSession.SetCapability(capability, TwainSetCapabilityMode.Set)Catch ex As ExceptionMessageBox.Show(ex.Message)End TryEnd Sub
private void radioNative_CheckedChanged(object sender, System.EventArgs e){try{TwainCapability capability = new TwainCapability();capability.Information.ContainerType = TwainContainerType.OneValue;capability.Information.Type = TwainCapabilityType.ImageTransferMechanism;capability.OneValueCapability.ItemType = TwainItemType.Uint16;capability.OneValueCapability.Value = (UInt16)TwainTransferMechanism.Native;_twnSession.SetCapability(capability, TwainSetCapabilityMode.Set);}catch (Exception ex){MessageBox.Show(this, ex.Message);}}
Add an event handler to the radioMemory CheckedChanged event and code it as follows:
Private Sub radioMemory_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radioMemory.CheckedChangedTryDim capability As New TwainCapabilitycapability.Information.ContainerType = TwainContainerType.OneValuecapability.Information.Type = TwainCapabilityType.ImageTransferMechanismcapability.OneValueCapability.ItemType = TwainItemType.Uint16capability.OneValueCapability.Value = CUShort(TwainTransferMechanism.Memory)_twnSession.SetCapability(capability, TwainSetCapabilityMode.Set)Catch ex As ExceptionMessageBox.Show(ex.Message)End TryEnd Sub
private void radioMemory_CheckedChanged(object sender, System.EventArgs e){try{TwainCapability capability = new TwainCapability();capability.Information.ContainerType = TwainContainerType.OneValue;capability.Information.Type = TwainCapabilityType.ImageTransferMechanism;capability.OneValueCapability.ItemType = TwainItemType.Uint16;capability.OneValueCapability.Value = (UInt16)TwainTransferMechanism.Memory;_twnSession.SetCapability(capability, TwainSetCapabilityMode.Set);}catch (Exception ex){MessageBox.Show(this, ex.Message);}}
Add an event handler to the radioFile CheckedChanged event and code it as follows:
Private Sub radioFile_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radioFile.CheckedChangedTryDim capability As New TwainCapabilitycapability.Information.ContainerType = TwainContainerType.OneValuecapability.Information.Type = TwainCapabilityType.ImageTransferMechanismcapability.OneValueCapability.ItemType = TwainItemType.Uint16capability.OneValueCapability.Value = CUShort(TwainTransferMechanism.File)_twnSession.SetCapability(capability, TwainSetCapabilityMode.Set)Catch ex As ExceptionMessageBox.Show(ex.Message)End TryEnd Sub
private void radioFile_CheckedChanged(object sender, System.EventArgs e){try{TwainCapability capability = new TwainCapability();capability.Information.ContainerType = TwainContainerType.OneValue;capability.Information.Type = TwainCapabilityType.ImageTransferMechanism;capability.OneValueCapability.ItemType = TwainItemType.Uint16;capability.OneValueCapability.Value = (UInt16)TwainTransferMechanism.File;_twnSession.SetCapability(capability, TwainSetCapabilityMode.Set);}catch (Exception ex){MessageBox.Show(this, ex.Message);}}