Take the following steps to create and run a program that acquires an image from a TWAIN source.
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:
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.
| Text | Name | Checked |
|---|---|---|
| Native | radioNative | False |
| Memory | radioMemory | False |
| File | radioFile | False |
Go to the toolbox (View->Toolbox) and drag and drop 4 instances of the Button control to the top of the form and set the following properties for them:
| Text | Name | |
|---|---|---|
| Acquire | buttonAcquire | |
| Select Source | buttonSelectSource | |
| Save Template File | buttonSaveTemplateFile | |
| Load Template File | buttonLoadTemplateFile |
Imports LeadtoolsImports Leadtools.Twain
using Leadtools;using Leadtools.Twain;
Private WithEvents _twnSession As TwainSession private TwainSession _twnSession; Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadTryDim myLicenseFile As String = "C:\LEADTOOLS 20\Support\Common\License\leadtools.lic"Dim myDeveloperKey As String = System.IO.File.ReadAllText("C:\LEADTOOLS 20\Support\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 20\Support\Common\License\leadtools.lic";string myDeveloperKey = System.IO.File.ReadAllText(@"C:\LEADTOOLS 20\Support\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);}}
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);}}
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;}
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);}}
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);}}
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);}}
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);}}
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);}}
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);}}
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);}}