Take the following steps to start a project and to add some code to obtain a Twain Capability:
- 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 "GetTwainCapability" 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 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, 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.Twain.dll
- Leadtools.Codecs.dll
- Leadtools.WinForms.dll
-
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 Select Source buttonSelectSource Use Capability buttonUseCapability Set Brightness buttonSetBrightness Acquire buttonAcquire -
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]
Imports Leadtools Imports Leadtools.Twain Imports Leadtools.Codecs Imports Leadtools.WinForms
-
Declare the following private variable:
[Visual Basic]
Private WithEvents twnSession As TwainSession
private TwainSession twnSession;
-
Add an event handler to the Form1 Load event and code it as
follows:
[Visual Basic]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try ' Unlock Document support. ' Note that this is a sample key, which will not work in your toolkit. RasterSupport.Unlock(RasterSupportType.Document, "TestKey") twnSession = new TwainSession twnSession.Startup(Me, "LEAD Technologies, Inc.", "LEAD Twain .NET", "Version 14", "LEADTools Twain test sample", TwainStartupFlags.None) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
private void Form1_Load(object sender, System.EventArgs e) { try { // Unlock Document support. // Note that this is a sample key, which will not work in your toolkit. RasterSupport.Unlock(RasterSupportType.Document, "TestKey"); twnSession = new TwainSession(); twnSession.Startup(this, "LEAD Technologies, Inc.", "LEAD Twain .NET", "Version 14", "LEADTools Twain test sample", TwainStartupFlags.None); } catch (Exception ex) { MessageBox.Show(this, ex.Message); } }
-
Add an event handler to the Form1 Closing event and code it as
follows:
[Visual Basic]
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing Try twnSession.Shutdown() Catch ex As Exception MessageBox.Show(ex.Message) End Try End 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 buttonSelectSource Click event and
code it as follows:
[Visual Basic]
Private Sub buttonSelectSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonSelectSource.Click Try twnSession.SelectSource(String.Empty) Catch ex As Exception MessageBox.Show(ex.Message) End Try End 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 buttonUseCapability Click event and
code it as follows:
[Visual Basic]
Private Sub buttonUseCapability_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonUseCapability.Click Try Dim twGetCapability As TwainCapability = twnSession.GetCapability(TwainCapabilityType.ImageTransferMechanism, TwainGetCapabilityMode.GetCurrent) If twGetCapability.Information.ContainerType = TwainContainerType.OneValue Then Dim capValue As TwainCapabilityValue = twGetCapability.OneValueCapability.Value If capValue <> TwainCapabilityValue.TransferMechanismFile Then Dim twSetCapability As TwainCapability twSetCapability = new TwainCapability twSetCapability.Information.ContainerType = TwainContainerType.OneValue twSetCapability.Information.Type = TwainCapabilityType.ImageTransferMechanism twSetCapability.OneValueCapability.ItemType = TwainItemType.Uint16 twSetCapability.OneValueCapability.Value = TwainCapabilityValue.TransferMechanismFile twnSession.SetCapability(twSetCapability, TwainSetCapabilityMode.Set) End If End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
private void buttonUseCapability_Click(object sender, System.EventArgs e) { try { TwainCapability twGetCapability = twnSession.GetCapability(TwainCapabilityType.ImageTransferMechanism, TwainGetCapabilityMode.GetCurrent); if (twGetCapability.Information.ContainerType == TwainContainerType.OneValue) { TwainCapabilityValue capValue = (TwainCapabilityValue)twGetCapability.OneValueCapability.Value; if (capValue != TwainCapabilityValue.TransferMechanismFile) { TwainCapability twSetCapability; twSetCapability = new TwainCapability(); twSetCapability.Information.ContainerType = TwainContainerType.OneValue; twSetCapability.Information.Type = TwainCapabilityType.ImageTransferMechanism; twSetCapability.OneValueCapability.ItemType = TwainItemType.Uint16; twSetCapability.OneValueCapability.Value = TwainCapabilityValue.TransferMechanismFile; twnSession.SetCapability(twSetCapability, TwainSetCapabilityMode.Set); } } } catch (Exception ex) { MessageBox.Show(this, ex.Message); } }
-
Add an event handler to the buttonSetBrightness Click event and
code it as follows:
[Visual Basic]
Private Sub buttonSetBrightness_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonSetBrightness.Click Try Dim twCapability As TwainCapability = new TwainCapability twCapability.Information.ContainerType = TwainContainerType.OneValue twCapability.Information.Type = TwainCapabilityType.ImageBrightness Dim brightneesValue As Integer = 50 twCapability.OneValueCapability.ItemType = TwainItemType.Fix32 twCapability.OneValueCapability.Value = brightneesValue twnSession.SetCapability(twCapability, TwainSetCapabilityMode.Set) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
private void buttonSetBrightness_Click(object sender, System.EventArgs e) { try { TwainCapability twCapability = new TwainCapability(); twCapability.Information.ContainerType = TwainContainerType.OneValue; twCapability.Information.Type = TwainCapabilityType.ImageBrightness; float brightneesValue = 50; twCapability.OneValueCapability.ItemType = TwainItemType.Fix32; twCapability.OneValueCapability.Value = brightneesValue; twnSession.SetCapability(twCapability, TwainSetCapabilityMode.Set); } catch (Exception ex) { MessageBox.Show(this, ex.Message); } }
-
Add an event handler to the buttonAcquire Click event and code
it as follows:
[Visual Basic]
Private Sub buttonAcquire_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonAcquire.Click Try Dim twnPros As TwainProperties = twnSession.Properties Dim dataTransfer As TwainDataTransferProperties = twnPros.DataTransfer dataTransfer.FileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "//LEADTOOLS Images//" & "Twain.bmp" twnPros.DataTransfer = dataTransfer twnSession.Properties = twnPros twnSession.Acquire(TwainUserInterfaceFlags.Show) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
private void buttonAcquire_Click(object sender, System.EventArgs e) { try { TwainProperties twnPros = twnSession.Properties; TwainDataTransferProperties dataTransfer = twnPros.DataTransfer; dataTransfer.FileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//LEADTOOLS Images//" + "Twain.bmp"; twnPros.DataTransfer = dataTransfer; twnSession.Properties = twnPros; twnSession.Acquire(TwainUserInterfaceFlags.Show); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
- Build, and Run the program to test it.