question

AMERSAID-7084 avatar image
0 Votes"
AMERSAID-7084 asked AMERSAID-7084 edited

close the twain scanner

hi


I used the following code to read the scanner file and review it in the image box with twain scanner.
It works well if the scanner is connected to the computer.
If the scanner is not connected, the code stops the window form and does not close.
I want a way to check the scanner connection or cancel if the scanner is not connected to the device (pc)

  Try
             Dim s As Collection
    
             s = SCANNER.ScanImages()
    
             For Each k In s
    
                 PictureBox1.Load(k)
    
             Next
         Catch ex As Exception
             MsgBox(ex.Message)
         End Try



dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
1 Vote"
Castorix31 answered AMERSAID-7084 edited

You should use WIA for scanners ("Windows Image Acquisition Library v2.0" on the COM tab),
more recent than Twain

There is also STI
A test with IStillImage.GetDeviceList to check if there is a scanner connected (it returns 0x80070491 with my scanner when I disconnect it) :

         Dim CLSID_StillImage As New Guid("B323F8E0-2E68-11D0-90EA-00AA0060F86C")
         Dim StillImageType As Type = Type.GetTypeFromCLSID(CLSID_StillImage, True)
         Dim StillImage As Object = Activator.CreateInstance(StillImageType)
         pStillImage = DirectCast(StillImage, IStillImage)
         If (pStillImage IsNot Nothing) Then
             Dim nNbItems As UInteger = 0
             Dim pBuffer As IntPtr = IntPtr.Zero
             '  HRESULT = 0x80070491  Message = Aucune correspondance pour la clé indiquée dans l'index. (Exception de HRESULT : 0x80070491)
             Dim hr As HRESULT = pStillImage.GetDeviceList(0, 0, nNbItems, pBuffer)
             If (hr <> HRESULT.S_OK) Then
                 MessageBox.Show("No scanner found", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
             Else
                 MessageBox.Show("Number of scanners detected : " & nNbItems.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
             End If
         End If

Simplified declarations :

     Public Enum HRESULT As Integer
         S_OK = 0
         S_FALSE = 1
         E_NOINTERFACE = &H80004002
         E_NOTIMPL = &H80004001
         E_FAIL = &H80004005
         E_UNEXPECTED = &H8000FFFF
         E_OUTOFMEMORY = &H8007000E
     End Enum
    
     <ComImport, Guid("641BD880-2DC8-11D0-90EA-00AA0060F86C"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
     Public Interface IStillImage
         Function Initialize(hinst As IntPtr, dwVersion As UInteger) As HRESULT
         <PreserveSig>
         Function GetDeviceList(dwType As UInteger, dwFlags As UInteger, ByRef pdwItemsReturned As UInteger, ByRef ppBuffer As IntPtr) As HRESULT
         ' incomplete, not used...
     End Interface




· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hi
Castorix31

The library you are referring to uses a higher version of Framework, and I want to work on the lowest possible version 3-3.5.
The following project is an example of the library used, which is a complete class.

simple full project

https://drive.google.com/file/d/1_WQdhexcq_-yOHdMGEPiIMvf2cjthtmF/view

0 Votes 0 ·

I don't use any library in the test with STI (IStillImage being old too) to check the scanners
(and WIA can be used with P/Invoke, no .NET Framework needed)

0 Votes 0 ·

wia Used

"Microsoft Windows Image Acquisition Library v2.0" COM component

net framework 4

0 Votes 0 ·
Show more comments

!
error when scanerjet connected or not connected..

119710-untitled.png


0 Votes 0 ·
untitled.png (129.5 KiB)
Show more comments
Viorel-1 avatar image
1 Vote"
Viorel-1 answered AMERSAID-7084 commented

Try something like this:

 Dim s As Collection
    
 Dim t As New Thread(Sub()
                         s = SCANNER.ScanImages()
                     End Sub)
 t.IsBackground = True
    
 t.Start()

 REM wait 20 seconds

 If Not t.Join(TimeSpan.FromSeconds(20)) Then
     t.Abort()
     MsgBox("Scanner is not responding")
 Else
     For Each k In s
         PictureBox1.Load(k)
     Next
 End If

Or check the SCANNER object and the documentation. Maybe there are more suitable methods.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hi

Viorel

In the event that the scanner is not connected, the code does not stop the device, and this is a good thing. But the main code also stopped and does not pull the image from the scanner..

simple full project

https://drive.google.com/file/d/1_WQdhexcq_-yOHdMGEPiIMvf2cjthtmF/view

0 Votes 0 ·