Click here to Skip to main content
13,867,032 members
Rate this:
Please Sign up or sign in to vote.
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
        WScript.Echo "Cancel scanning by user"
    End If
    WScript.Echo "No connected devices"
End If
Set objDeviceManager = Nothing
Set objDeviceInfos   = Nothing
WScript.Quit 0

Translated this scrip to

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

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

Code on
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.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
                End If


                objDevice = Nothing
            End If
        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.
Updated 27-Nov-17 3:08am
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 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

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.
__Radik__ 21-Nov-17 5:17am
Thank you
Karthik_Mahalingam 21-Nov-17 5:20am
Welcome. Take care on further posts

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

 Private WiaItem As WIA.Item = Nothing

Private Sub Scan_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            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'
                    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
        Catch ex As Exception
            MsgBox("Check scaner or USB")
        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
            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?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190214.1 | Last Updated 27 Nov 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100