Set/Get a Twain Capability (Visual Basic)

Take the following steps to start a project and to add some code to use the Get/Set capability methods and then acquire your image using the newly set capability:

1.

Start Visual Basic.

2.

Add the LEAD RasterTwain Object Library and LEAD Raster Variant Object Library to your project.

 

On the Project pull-down menu, use the References option, and select the LEAD RasterTwain Object Library (14.5).

 

On the Project pull-down menu, use the References option, and select the LEAD Raster Variant Object Library (14.5).

3.

Add the LEAD Raster Object Library to your project. On the Project pull-down menu, use the References option, and select the LEAD Raster Object Library (14.5).

4.

Add the following code to the general declarations section of your project:

Public TwnGetCap As New LEADTwainCapability
Public TwnSetCap As New LEADTwainCapability
Public WithEvents RasterTwain As LEADRasterTwain
Public nXferMode As Integer
Public nRet As Integer

5.

Add the following code to the main form's Load procedure:

Set RasterTwain = New LEADRasterTwain
RasterTwain.InitSession hWnd
RasterTwain.EnableMethodErrors = False

'   Unlock Document support.
'   Note that this is a sample key, which will not work in your toolkit. 
RasterTwain.UnlockSupport L_SUPPORT_DOCUMENT, "TestKey"

6.

Add the following code to the main form's Unload procedure:

RasterTwain.EndSession
Set RasterTwain = Nothing

7.

At the top of your main form, add 4 command buttons and name them as follows:

 

Name

Caption

 

cmdSelectSource

Select Source

 

cmdUseCapability

Use Capability

 

cmdSetBrightness

Set Brightness

 

cmdAcquire

Acquire

8.

Code the "Select Source" control's Click procedure as follows. In online help, you can use the Edit pull-down menu to copy the block of code.

Private Sub cmdSelectSource_Click()
  RasterTwain.SelectSource
End Sub

9.

Code the "Use Capability" control's Click procedure as follows. In online help, you can use the Edit pull-down menu to copy the block of code.

Private Sub cmdUseCapability_Click()
   Dim Xfer As New LEADRasterVariant

   TwnGetCap.EnableMethodErrors = False
   TwnGetCap.CapInfo.ConType = L_TWON_ONEVALUE
   TwnGetCap.CapInfo.Capability = L_ICAP_XFERMECH

   nRet = RasterTwain.GetCapability2 (TwnGetCap, L_LTWAIN_CAPABILITY_GETCURRENT)
   If nRet = 0 Then
      Xfer = TwnGetCap.CapOneValue.OneValCapValue
If Xfer.LongValue <> L_TWSX_FILE Then
         TwnSetCap.EnableMethodErrors = False
         TwnSetCap.CapInfo.Capability= L_ICAP_XFERMECH
         TwnSetCap.CapInfo.ConType = L_TWON_ONEVALUE

Xfer.Type = VALUE_USHORT
Xfer.LongValue = L_TWSX_FILE
         TwnSetCap.CapOneValue.OneValItemType = L_TWTY_UINT16
         TwnSetCap.CapOneValue.OneValCapValue= Xfer

         nRet = RasterTwain.SetCapability2 (TwnSetCap, L_LTWAIN_CAPABILITY_SET)
      Else
         MsgBox "The Transfer mode is File"
      End If
   Else
      MsgBox "Error occurred in the GetCapability method"
   End If
End Sub

10.

Code the "Set Brightness" control's Click procedure as follows. In online help, you can use the Edit pull-down menu to copy the block of code.

Private Sub cmdSetBrightness_Click()
   Dim iRet As Integer
   Dim CapVal As New LEADRasterVariant
   Dim TwainCap As New LEADTwainCapability

   TwainCap.CapInfo.ConType = L_TWON_ONEVALUE
   TwainCap.CapInfo.Capability = L_ICAP_BRIGHTNESS
   TwainCap.EnableMethodErrors = False
   CapVal.Type = VALUE_FLOAT
   CapVal.FloatValue = 50#
   TwainCap.CapOneValue.OneValItemType = L_TWTY_FIX32
   TwainCap.CapOneValue.OneValCapValue = CapVal
   iRet = RasterTwain.SetCapability2 (TwainCap, L_LTWAIN_CAPABILITY_SET)
   If (iRet <> 0) Then
      MsgBox "Error Setting Capability"
   End If
   Set TwainCap = Nothing
End Sub

 

11.

Code the "Acquire" control's Click procedure as follows. In online help, you can use the Edit pull-down menu to copy the block of code.

Private Sub cmdAcquire_Click()
   RasterTwain.FileTransferName = "c:\twain.bmp"
   nRet = RasterTwain.Acquire (L_LTWAIN_SHOW_USER_INTERFACE)
   If nRet <> 0 Then
      MsgBox "Error acquiring from source"
   End If
End Sub

12.

Save your project and run your program to test it.