' Utilizzare periferiche di acquisizione da codice Visual Basic - Listato 4
'==========================================================================
<CLSCompliant(True)> Public NotInheritable Class EZTwain
Private Const TWAIN_BW = &H1 ' 1-bit per pixel, Bianco e Nero
Private Const TWAIN_GRAY = &H2 ' 1,4, o 8 bit Scala di grigi (= TWPT_GRAY)
Private Const TWAIN_RGB = &H4 ' 24 bit true color (= TWPT_RGB)
Private Const TWAIN_PALETTE = &H8 ' palette a 1,4, o 8 bit (= TWPT_PALETTE)
Private Const TWAIN_ANYTYPE = &H0 ' qualsiasi di quelli sopra
Public Enum PixelTypes
BW = TWAIN_BW
GRAYSCALE = TWAIN_GRAY
RGB = TWAIN_RGB
PALETTE = TWAIN_PALETTE
ANYTYPE = TWAIN_ANYTYPE
End Enum
Public Shared Function SelectTwainsource(ByVal handle As Integer) As Integer
Return NativeMethods.TWAIN_SelectImageSource(handle)
End Function
Public Shared Function AcquireToFileName(ByVal handle As Integer, _
ByVal outputFileName As String) As Integer
Return NativeMethods.TWAIN_AcquireToFilename(handle, outputFileName)
End Function
Public Overloads Shared Function AcquireNative _
(ByVal outputFileName As String) As Integer
Dim lnImageHandle As Integer = NativeMethods.TWAIN_AcquireNative(0, 0)
Dim lnReply = NativeMethods.TWAIN_WriteNativeToFilename _
(lnImageHandle, outputFileName)
NativeMethods.TWAIN_FreeNative(lnImageHandle)
Return lnReply
End Function
Public Overloads Shared Function AcquireNative _
(ByVal PixelType As PixelTypes) As String
Dim Filename = IO.Path.GetTempFileName
Dim lnImageHandle As Integer = _
NativeMethods.TWAIN_AcquireNative(0, PixelType)
Dim lnReply = NativeMethods.TWAIN_WriteNativeToFilename _
(lnImageHandle, Filename)
NativeMethods.TWAIN_FreeNative(lnImageHandle)
If lnReply = 0 Then
Return Filename
Else
Return ""
End If
End Function
Public Shared Function AcquireToClipboard(ByVal handle As Integer,
ByVal PixelType As PixelTypes) As Integer
Dim nClip = NativeMethods.TWAIN_AcquireToClipboard(handle, PixelType)
Return nClip
End Function
Public Shared ReadOnly Property Version() As String
Get
Return (NativeMethods.TWAIN_EasyVersion / _
100).ToString(System.Globalization.CultureInfo.InvariantCulture)
End Get
End Property
'Determina se i servizi Twain sono attivi e restituisce Vero o Falso
Private Shared Function Availability() As Boolean
If NativeMethods.TWAIN_IsAvailable = 0 Then
Return False
Else
Return True
End If
End Function
Public Shared ReadOnly Property IsAvailable() As Boolean
Get
Return Availability()
End Get
End Property
Public Shared Function SetBrightness(ByVal value As Double) As Integer
Return NativeMethods.TWAIN_SetBrightness(value)
End Function
Public Shared Function SetContrast(ByVal value As Double) As Integer
Return NativeMethods.TWAIN_SetContrast(value)
End Function
Public Shared Function SetCurrentResolution _
(ByVal value As Double) As Integer
Return NativeMethods.TWAIN_SetCurrentResolution(value)
End Function
'Le proprietà devono essere preferite alle funzioni/sub quando
'svolgono compiti di Get..
Public Shared ReadOnly Property GetCurrentResolution() As Double
Get
Return NativeMethods.TWAIN_GetCurrentResolution
End Get
End Property
Public Shared ReadOnly Property GetPixelType() As Integer
Get
Return NativeMethods.TWAIN_GetPixelType
End Get
End Property
Public Shared Function SetCurrentPixelType _
(ByVal value As Integer) As Integer
Return NativeMethods.TWAIN_SetCurrentPixelType(value)
End Function
Public Shared ReadOnly Property GetBitDepth() As Integer
Get
Return NativeMethods.TWAIN_GetBitDepth
End Get
End Property
Public Shared Function SetBitDepth(ByVal value As Integer) As Integer
Return NativeMethods.TWAIN_SetBitDepth(value)
End Function
Public Shared Function IsTwainInstalled() As Boolean
Dim Exists As Boolean
With My.Computer.FileSystem
Exists = .FileExists(Environment.SystemDirectory + "\eztw32.dll")
End With
Return Exists
End Function
'In una classe che espone metodi Shared, il costruttore deve essere
'vuoto e privato
Private Sub New()
End Sub
End Class