שתף באמצעות


Use WIA Scan Dialog without preview

Question

Saturday, September 7, 2019 1:32 PM

Hi there.

I'm using WIA to scan images before I run an OCR module.

The application is for visually impaired and or blind people who cannot handle a visual dialog.

I know about the possibility to disable the dialog e.g. by a code like:

Scanner.ShowUI = False

Unfortuneately this command seems to be a control sensitive command from ScannerActiveX which does not work longer in my PC (maybe to old?? And not compatible with Windows 10 64).

Im using  the public function see below:

        '**Quelle: http://deunkel.blogspot.com/2010/04/image-scan-vbnet-using-wia-windows-7.html
        'Pfad zur Temp Datei des erfassten Bildes 
        Dim strTempImagePath As String = String.Empty
        'Windows GUI zur Interaktion mit dem Gerät 

        Dim wiaDialog As New WIA.CommonDialog

        'Fest vorgegebene Werte 


        'Variable für das erfasste Bild 
        Dim wiaImage As WIA.ImageFile = Nothing
        Try
            'Aufruf der Windows GUI zur Interaktion mit dem Gerät, 
            'rückgabe ist das erfasste Bild
            wiaImage = wiaDialog.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType, , , , , )


        Catch ex As Exception
            MsgBox("Ein Fehler ist aufgetreten! Überprüfen Sie ob das Gerät Eingeschaltet und angeschlossen ist." & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try
        If Not wiaImage Is Nothing Then

            'Bild in Temporere Datei Speichern
            strTempImagePath = ScanDir & "\LastScan.bmp"

            If File.Exists(strTempImagePath) Then
                My.Computer.FileSystem.DeleteFile(strTempImagePath)
            End If
            wiaImage.SaveFile(strTempImagePath)



        Else
            MsgBox("Es wurde kein Bild erfasst!", MsgBoxStyle.Information, "Information")
        End If
        Return strTempImagePath
    End Function

Does there exist an easy way/method to set the scanner settings like:

resolution= 300 
ImageType = Grayscale  ...

and scan whitout dialog.

Liebe Grüße Stefan

All replies (8)

Saturday, September 7, 2019 2:41 PM ✅Answered | 1 vote

This scans without dialog (Windows 10, VS 2015) =>

Dim dm As DeviceManager = New DeviceManager()
Dim device As Device = Nothing
For Each deviceinfo As DeviceInfo In dm.DeviceInfos
    ' Connect to first scanner found
    device = deviceinfo.Connect()
    Exit For
Next
Dim bHasMorePages As Boolean = True
Dim n As Integer = 0
While bHasMorePages
    Dim image As WIA.ImageFile = Nothing
    Dim Item As WIA.Item = TryCast(device.Items(1), WIA.Item)
    Try
        Dim WiaCommonDialog As WIA.CommonDialog = New CommonDialog()
        image = DirectCast(WiaCommonDialog.ShowTransfer(Item, WIA.FormatID.wiaFormatJPEG, False), ImageFile)
        ' Save to file
        Dim sFileName As String = "E:\Picture" & n.ToString() & ".jpg"
        If File.Exists(sFileName) Then
            File.Delete(sFileName)
        End If
        image.SaveFile(sFileName)
        image = Nothing
    Catch ex As Exception
        MessageBox.Show("Error : " + ex.Message)
    Finally
        Item = Nothing
        Dim documentHandlingSelect As [Property] = Nothing
        Dim documentHandlingStatus As [Property] = Nothing
        For Each prop As [Property] In device.Properties
            If prop.PropertyID = WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT Then
                documentHandlingSelect = prop
            End If
            If prop.PropertyID = WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS Then
                documentHandlingStatus = prop
            End If
        Next
        bHasMorePages = False
        If documentHandlingSelect IsNot Nothing Then
            If (Convert.ToUInt32(documentHandlingSelect.Value()) And WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) <> 0 Then
                bHasMorePages = ((Convert.ToUInt32(documentHandlingStatus.Value()) And WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) <> 0)
            End If
        End If
        n += 1
    End Try
End While

Sunday, September 8, 2019 11:50 AM ✅Answered | 1 vote

The declarations  are :

    Private Class WIA_DPS_DOCUMENT_HANDLING_SELECT
        Public Const FEEDER As UInteger = &H1
        Public Const FLATBED As UInteger = &H2
    End Class

    Private Class WIA_DPS_DOCUMENT_HANDLING_STATUS
        Public Const FEED_READY As UInteger = &H1
    End Class

    Private Class WIA_PROPERTIES
        Public Const WIA_RESERVED_FOR_NEW_PROPS As UInteger = 1024
        Public Const WIA_DIP_FIRST As UInteger = 2
        Public Const WIA_DPA_FIRST As UInteger = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS
        Public Const WIA_DPC_FIRST As UInteger = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS
        Public Const WIA_DPS_FIRST As UInteger = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS
        Public Const WIA_DPS_DOCUMENT_HANDLING_STATUS As UInteger = WIA_DPS_FIRST + 13
        Public Const WIA_DPS_DOCUMENT_HANDLING_SELECT As UInteger = WIA_DPS_FIRST + 14
    End Class

    Private Class WIA_ERRORS
        Public Const BASE_VAL_WIA_ERROR As UInteger = &H80210000UI
        Public Const WIA_ERROR_PAPER_EMPTY As UInteger = BASE_VAL_WIA_ERROR + 3
    End Class

Sunday, September 8, 2019 2:10 PM ✅Answered | 1 vote

These problems are these lines:

Dim device As Device = Nothing
...
Dim WiaCommonDialog As WIA.CommonDialog = New CommonDialog()

The description says:

"Device" is ambiguously imported from the namespaces or types "Viscomsoft.Video.Capture, WIA"

"Common" dialog st ambiguously imported from the namespaces or types

"System.Window.Forms, WIA"

Do you have an idea how to solve this?

Prefix them : 

**WIA.Device **

WIA.CommonDialog


Sunday, September 8, 2019 4:06 PM ✅Answered | 1 vote

This code scans whitout dialog. Great.

The result is a TrueColor Image.

Can I change this e.g. to Black/White or Grayscale  in 300 dpi resolution?

You can convert WIA.ImageFile into System.Drawing.Image, then you can do any transformation

For example, for greyscale, main part of code :

' Modified code (image renamed into imageFile)
Dim WiaCommonDialog As WIA.CommonDialog = New WIA.CommonDialog()
imageFile = DirectCast(WiaCommonDialog.ShowTransfer(Item, WIA.FormatID.wiaFormatJPEG, False), WIA.ImageFile)

Dim imageBytes = CType(imageFile.FileData.BinaryData, Byte())
Dim ms = New MemoryStream(imageBytes)
Dim img As System.Drawing.Image = Image.FromStream(ms)

Dim newBitmap As Bitmap = New Bitmap(imageFile.Width, imageFile.Height)
Using g As Graphics = Graphics.FromImage(newBitmap)
    Dim colorMatrix As ColorMatrix = New ColorMatrix(New Single()() {New Single() {0.3F, 0.3F, 0.3F, 0, 0}, New Single() {0.59F, 0.59F, 0.59F, 0, 0}, New Single() {0.11F, 0.11F, 0.11F, 0, 0}, New Single() {0, 0, 0, 1, 0}, New Single() {0, 0, 0, 0, 1}})
    Dim imageAttributes As ImageAttributes = New ImageAttributes
    imageAttributes.SetColorMatrix(colorMatrix)
    g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imageAttributes)
End Using

Dim sFileName2 As String = "E:\PictureGrey" & n.ToString() & ".jpg"
newBitmap.Save(sFileName2, System.Drawing.Imaging.ImageFormat.Jpeg)

Sunday, September 8, 2019 11:20 AM

Hi Castorix31

I declared WIA in my application but i still have problems with

WIA_PROPERTIES and

WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER

I don't know which declaration or control is requiered.

Could you please show yor Import?

Liebe Grüße Stefan


Sunday, September 8, 2019 1:01 PM

Thanks a lot.

This code scans whitout dialog. Great.

The result is a TrueColor Image.

Can I change this e.g. to Black/White or Grayscale  in 300 dpi resolution?

Liebe Grüße Stefan


Sunday, September 8, 2019 1:45 PM

Castorix31,

Sorry, I have an additioanal question/problem. When I start your code in a test application everything ist ok.

When I include the code to my own program I receive an error message:

  Dim dm As DeviceManager = New DeviceManager()
        Dim device As Device = Nothing
        For Each deviceinfo As DeviceInfo In dm.DeviceInfos
            ' Connect to first scanner found
            device = deviceinfo.Connect()
            Exit For
        Next
        Dim bHasMorePages As Boolean = True
        Dim n As Integer = 0
        While bHasMorePages
            Dim image As WIA.ImageFile = Nothing
            Dim Item As WIA.Item = TryCast(device.Items(1), WIA.Item)
            Try
                Dim WiaCommonDialog As WIA.CommonDialog = New CommonDialog()
                image = DirectCast(WiaCommonDialog.ShowTransfer(Item, WIA.FormatID.wiaFormatJPEG, False), ImageFile)
                ' Save to file
                Dim sFileName As String = "C:\EasyBig\Images\Picture" & n.ToString() & ".jpg"
                If File.Exists(sFileName) Then
                    File.Delete(sFileName)
                End If
                image.SaveFile(sFileName)
                image = Nothing
            Catch ex As Exception
                MessageBox.Show("Error : " + ex.Message)
            Finally
                Item = Nothing
                Dim documentHandlingSelect As [Property] = Nothing
                Dim documentHandlingStatus As [Property] = Nothing
                For Each prop As [Property] In device.Properties
                    If prop.PropertyID = WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT Then
                        documentHandlingSelect = prop
                    End If
                    If prop.PropertyID = WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS Then
                        documentHandlingStatus = prop
                    End If
                Next
                bHasMorePages = False
                If documentHandlingSelect IsNot Nothing Then
                    If (Convert.ToUInt32(documentHandlingSelect.Value()) And WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) <> 0 Then
                        bHasMorePages = ((Convert.ToUInt32(documentHandlingStatus.Value()) And WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) <> 0)
                    End If
                End If
                n += 1
            End Try
        End While

These problems are these lines:

Dim device As Device = Nothing
...
Dim WiaCommonDialog As WIA.CommonDialog = New CommonDialog()

The description says:

"Device" is ambiguously imported from the namespaces or types "Viscomsoft.Video.Capture, WIA"

"Common" dialog st ambiguously imported from the namespaces or types

"System.Window.Forms, WIA"

Do you have an idea how to solve this?

Liebe Grüße Stefan


Sunday, September 8, 2019 4:20 PM

Solved. 

But the progressbar shows no change while the scanner is in progress.



And the saved image is a color image. I know, I asked for a method to scan without dialog. I want to define the settings by my code. 

Possible?

Liebe Grüße Stefan