Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was looking for a way to scan using .net. Found such a script
This script works correctly with the Brother 7057 and with the Canon MF 3228


Option Explicit
 
Const ScannerDeviceType = 1
Const ColorIntent = 1
Const wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
 
 
Dim objDeviceManager
Dim objDeviceInfos
Dim objDevice
 
Dim objImageFile
Dim objImageProcess
 
Dim strPath2Save
Dim strFormat
Dim intQuality
Dim intDPI
Dim intHorizontalSize
Dim intVerticalSize
 
strPath2Save      = "D:\MyImage.jpg"      ' Полное имя файла для сохранения
strFormat         = wiaFormatJPEG         ' Формат файла — *.jpg
intQuality        = 85                    ' Качество jpg
intDPI            = 150                   ' Разрешение — 150 dpi
 
Set objDeviceManager = WScript.CreateObject("WIA.DeviceManager")
Set objDeviceInfos = objDeviceManager.DeviceInfos
 
If objDeviceInfos.Count > 0 Then

     Set objDevice = WScript.CreateObject("WIA.CommonDialog").ShowSelectDevice(ScannerDeviceType, False, False)
    If Not objDevice Is Nothing Then
        With objDevice
            With .Items(1)
                With .Properties
                    .Item("6146").Value = ColorIntent             
                    .Item("6147").Value = intDPI                  
                    .Item("6148").Value = intDPI                  
                    .Item("6149").Value = 0                       
                    .Item("6150").Value = 0                       
                    .Item("6151").Value = 1240
                    .Item("6152").Value = 1748
                End With
                
                Set objImageFile = .Transfer()
                
                
                Set objImageProcess = WScript.CreateObject("WIA.ImageProcess")
                
                With objImageProcess
                    With .Filters
                        .Add objImageProcess.FilterInfos("Convert").FilterID
                        
                        With .Item(1).Properties
                            .Item("FormatID").Value = strFormat
                            .Item("Quality").Value  = intQuality
                        End With
                    End With
                    
                    Set objImageFile = .Apply(objImageFile)
                End With
            End With
        End With

        With WScript.CreateObject("Scripting.FileSystemObject")
            If .FileExists(strPath2Save) Then
                .DeleteFile strPath2Save
            End If
        End With
        
        objImageFile.SaveFile strPath2Save
        
        WScript.Echo "Complete."
        
        Set objDevice = Nothing
    Else
        WScript.Echo "Cancel scanning by user"
    End If
Else
    WScript.Echo "No connected devices"
End If
 
Set objDeviceManager = Nothing
Set objDeviceInfos   = Nothing
 
WScript.Quit 0


Translated this scrip to vb.net

With the Broteher 7057 everything works fine
With the Canon MF 3228, when you try to scan in color, you get a black and white image with the entire scanned area (1240x1748), and if you try to scan in grayscale, you will get a gray drawing, but half of the scan area (620x874)

Please, help me with my problem

PS
English - is not my first languge, sorry
For work this in vb.net need wiaaut.dll


PSS
Code on vb.net
Public Sub Scan()
        Const ScannerDeviceType = 1
        Const ColorIntent = 1
        Const wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"

        Dim strPath2Save As String = "D:\MyImage.jpg"      
        Dim strFormat As String = wiaFormatJPEG         
        Dim intQuality As Integer = 85                  
        Dim intDPI As Integer = 150                   
        Dim intHorizontalSize As Integer = 1240 
        Dim intVerticalSize As Integer = 1748 


        Dim objDeviceManager As WIA.DeviceManager = CreateObject("WIA.DeviceManager")
        Dim objDeviceInfos = objDeviceManager.DeviceInfos

        If objDeviceInfos.Count > 0 Then

            Dim objDevice = CreateObject("WIA.CommonDialog").ShowSelectDevice(ScannerDeviceType, False, False)

            If Not objDevice Is Nothing Then
                objDevice.Items(1).Properties.Item("6146").Value = ColorIntent
                objDevice.Items(1).Properties.Item("6147").Value = intDPI
                objDevice.Items(1).Properties.Item("6148").Value = intDPI
                objDevice.Items(1).Properties.Item("6149").Value = 0
                objDevice.Items(1).Properties.Item("6150").Value = 0
                objDevice.Items(1).Properties.Item("6151").Value = intHorizontalSize
                objDevice.Items(1).Properties.Item("6152").Value = intVerticalSize

                Dim objImageFile As WIA.ImageFile = objDevice.Items(1).Transfer()
                Dim objImageProcess As WIA.ImageProcess = CreateObject("WIA.ImageProcess")

                'objImageProcess.Filters.FilterInfos("Convert").FilterID()

                'objImageProcess.Item(1).Properties.Item("FormatID").Value = strFormat
                'objImageProcess.Item(1).Properties.Item("Quality").Value = intQuality


                ' objImageFile = objImageProcess.Apply(objImageFile)


                If CreateObject("Scripting.FileSystemObject").FileExists(strPath2Save) Then
                    CreateObject("Scripting.FileSystemObject").DeleteFile(strPath2Save)
                End If


                objImageFile.SaveFile(strPath2Save)

                objDevice = Nothing
            Else
            End If
        Else
        End If

        objDeviceManager = Nothing
        objDeviceInfos = Nothing


    End Sub


What I have tried:

1) I called the technical support of Canon, but they do not deal with such problems
2) If you run the script as a scan.vbs file from the program itself, it works, but there is no feedback from the program and many other errors may appear while the scanning process is in progress.
Posted
Updated 27-Nov-17 2:08am
v3
Comments
Richard Deeming 20-Nov-17 11:16am    
If the problem is with your VB.NET translation of the code, then why have you not shown us that code?

Click "Improve question" and add the VB.NET version of your code to the question.
__Radik__ 21-Nov-17 5:16am    
Add vb.net code
Richard Deeming 21-Nov-17 11:58am    
The obvious place to start would be the "Filters" code, which is present in your VBScript, but commented out in your VB.NET code.
__Radik__ 22-Nov-17 0:54am    
Believe me on the floor that the filter is not important here, it's not the reason. I want to try changing the code so that the settings for the scan parameters are selected by the user through the standard wia interface. But only in the evening I can do it.



I did it!
Everything is working.
Scans in color mode.
But, in this version of the program, an extra dialogue window appeared from the WIA with the choice of scan parameters.
But you can save these parameters to a global variable and use it, without dialog box from the WIA, throughout the program's operation - until the program closes.


I nevertheless turned to the technical support of Canon with this question. And do you know what they said to me?
"Your printer Canon MF3228 is not network." Sincerely, Canon.

What does "network - not network"?

I wrote to them about the error in scanning through vb.net

and they answer me "Your printer is not networked"
no words only emotions...
Karthik_Mahalingam 21-Nov-17 3:23am    
use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

1 solution

 Private WiaItem As WIA.Item = Nothing

Private Sub Scan_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim CommonDialog12 As New WIA.CommonDialog
            Dim Device12a As WIA.Device = CommonDialog12.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, False, False)
            For i As Integer = 0 To 35 '35-Properties count'
                Try
                    If Device12a.Properties.Item(i).Value.ToString.Contains("Canon") Then
                        WiaItem = Device12a.Items(1) 'WiaItem - Global var'
                        CommonDialog12.ShowItemProperties(WiaItem, False)
                        Exit For
                    End If
                Catch ex As Exception
                End Try
            Next
        Catch ex As Exception
            MsgBox("Check scaner or USB")
            Me.Close()
        End Try
    End Sub

 Private Sub scan(Color As Integer, x2 As Integer, y2 As Integer, DPI As Integer, Optional W As Integer = 1, Optional H As Integer = 1) 'W=1 H=2
        
        Try
            Dim CommonDialog1 As New WIA.CommonDialog
            Dim Device1a As WIA.Device = CommonDialog1.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, False, False)
            Dim newItem As WIA.Item = Device1a.Items(1)
            If WiaItem IsNot Nothing Then
                newItem = WiaItem 'If Canon'
            End If
............
end sub




I have a question: is it possible in some way to save this WiaItem to a file or registry or something like that to immediately use it without an additional dialog box?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900