Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4100428
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:33:56+00:00 2026-05-20T20:33:56+00:00

I’m building a .Net 4.0 application for remote control of a scanner device. I

  • 0

I’m building a .Net 4.0 application for remote control of a scanner device. I have tried both TWAIN and WIA libraries, but I have the same problem. Scanning images without scanner selection and scanning settings dialogs.

I found a useful article on WIA scripting in .Net, and modified it to this:

private Image Scan(string deviceName)
{
    WiaClass wiaManager = null;       // WIA manager COM object
    CollectionClass wiaDevs = null;   // WIA devices collection COM object
    ItemClass wiaRoot = null;         // WIA root device COM object
    CollectionClass wiaPics = null;   // WIA collection COM object
    ItemClass wiaItem = null;         // WIA image COM object

    try
    {
        // create COM instance of WIA manager
        wiaManager = new WiaClass();

        // call Wia.Devices to get all devices
        wiaDevs = wiaManager.Devices as CollectionClass;
        if ((wiaDevs == null) || (wiaDevs.Count == 0))
        {
            throw new Exception("No WIA devices found!");
        }

        object device = null;
        foreach (IWiaDeviceInfo currentDevice in wiaManager.Devices)
        {
            if (currentDevice.Name == deviceName)
            {
                device = currentDevice;
                break;
            }
        }

        if (device == null)
        {
            throw new Exception
            (
                "Device with name \"" + 
                deviceName + 
                "\" could not be found."
            );
        }

        // select device
        wiaRoot = (ItemClass)wiaManager.Create(ref device); 

        // something went wrong
        if (wiaRoot == null)
        {
            throw new Exception
            (
                "Could not initialize device \"" + 
                deviceName + "\"."
            );
        }

        wiaPics = wiaRoot.GetItemsFromUI
        (
            WiaFlag.SingleImage,
            WiaIntent.ImageTypeColor
        ) as CollectionClass;

        if (wiaPics == null || wiaPics.Count == 0)
        {
            throw new Exception("Could not scan image.");
        }

        Image image = null;

        // enumerate all the pictures the user selected
        foreach (object wiaObj in wiaPics)
        {
            if (image == null)
            {
                wiaItem = (ItemClass)Marshal.CreateWrapperOfType
                (
                    wiaObj, typeof(ItemClass)
                );

                // create temporary file for image
                string tempFile = Path.GetTempFileName();

                // transfer picture to our temporary file
                wiaItem.Transfer(tempFile, false);

                // create Image instance from file
                image = Image.FromFile(tempFile);
            }

            // release enumerated COM object
            Marshal.ReleaseComObject(wiaObj);
        }

        if (image == null)
        {
            throw new Exception("Error reading scanned image.");
        }

        return image;
    }
    finally
    {
        // release WIA image COM object
        if (wiaItem != null)
            Marshal.ReleaseComObject(wiaItem);

        // release WIA collection COM object
        if (wiaPics != null)
            Marshal.ReleaseComObject(wiaPics);

        // release WIA root device COM object
        if (wiaRoot != null)
            Marshal.ReleaseComObject(wiaRoot);

        // release WIA devices collection COM object
        if (wiaDevs != null)
            Marshal.ReleaseComObject(wiaDevs);

        // release WIA manager COM object
        if (wiaManager != null)
            Marshal.ReleaseComObject(wiaManager);
    }
}

With this I actually managed to select the device from configuration (input parameter of the Scan method) and retrieve the resulting image after scan.

But the problem with scanning options dialog (Scan using DEVICENAME). As this is a remote control application, dialog will not be visible to the user, so I need to either skip it using default settings, or use settings from a configuration if necessary.

Scanning options dialog:
scanning options dialog

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:33:56+00:00Added an answer on May 20, 2026 at 8:33 pm

    In the end I did not use the code written in the question for scanning dialogs. I found a useful example of Scanning with Windows Image Acquisition 2.0 which by the way also had a blocking dialog, but this was easily modified and in moments I had a simple class with a Scan(string scannerId) function which would just scan with a selected device and nothing more, see code () below:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Drawing;
    
    namespace WIATest
    {
        class WIAScanner
        {
            const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
    
            class WIA_DPS_DOCUMENT_HANDLING_SELECT
            {
                public const uint FEEDER = 0x00000001;
                public const uint FLATBED = 0x00000002;
            }
    
            class WIA_DPS_DOCUMENT_HANDLING_STATUS
            {
                public const uint FEED_READY = 0x00000001;
            }
    
            class WIA_PROPERTIES
            {
                public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024;
                public const uint WIA_DIP_FIRST = 2;
                public const uint WIA_DPA_FIRST = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
                public const uint WIA_DPC_FIRST = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
                //
                // Scanner only device properties (DPS)
                //
                public const uint WIA_DPS_FIRST = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
                public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_DPS_FIRST + 13;
                public const uint WIA_DPS_DOCUMENT_HANDLING_SELECT = WIA_DPS_FIRST + 14;
            }
    
            /// <summary>
            /// Use scanner to scan an image (with user selecting the scanner from a dialog).
            /// </summary>
            /// <returns>Scanned images.</returns>
            public static List<Image> Scan()
            {
                WIA.ICommonDialog dialog = new WIA.CommonDialog();
                WIA.Device device = dialog.ShowSelectDevice(WIA.WiaDeviceType.UnspecifiedDeviceType, true, false);
    
                if (device != null)
                {
                    return Scan(device.DeviceID);
                }
                else
                {
                    throw new Exception("You must select a device for scanning.");
                }
            }
    
            /// <summary>
            /// Use scanner to scan an image (scanner is selected by its unique id).
            /// </summary>
            /// <param name="scannerName"></param>
            /// <returns>Scanned images.</returns>
            public static List<Image> Scan(string scannerId)
            {
                List<Image> images = new List<Image>();
    
                bool hasMorePages = true;
                while (hasMorePages)
                {
                    // select the correct scanner using the provided scannerId parameter
                    WIA.DeviceManager manager = new WIA.DeviceManager();
                    WIA.Device device = null;
    
                    foreach (WIA.DeviceInfo info in manager.DeviceInfos)
                    {
                        if (info.DeviceID == scannerId)
                        {
                            // connect to scanner
                            device = info.Connect();
                            break;
                        }
                    }
    
                    // device was not found
                    if (device == null)
                    {
                        // enumerate available devices
                        string availableDevices = "";
                        foreach (WIA.DeviceInfo info in manager.DeviceInfos)
                        {
                            availableDevices += info.DeviceID + "n";
                        }
    
                        // show error with available devices
                        throw new Exception("The device with provided ID could not be found. Available Devices:n" + availableDevices);
                    }
    
                    WIA.Item item = device.Items[1] as WIA.Item;
    
                    try
                    {
                        // scan image
                        WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();
                        WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false);
    
                        // save to temp file
                        string fileName = Path.GetTempFileName();
                        File.Delete(fileName);
                        image.SaveFile(fileName);
                        image = null;
    
                        // add file to output list
                        images.Add(Image.FromFile(fileName));
                    }
                    catch (Exception exc)
                    {
                        throw exc;
                    }
                    finally
                    {
                        item = null;
    
                        //determine if there are any more pages waiting
                        WIA.Property documentHandlingSelect = null;
                        WIA.Property documentHandlingStatus = null;
    
                        foreach (WIA.Property prop in device.Properties)
                        {
                            if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                                documentHandlingSelect = prop;
    
                            if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                                documentHandlingStatus = prop;
                        }
    
                        // assume there are no more pages
                        hasMorePages = false;
    
                        // may not exist on flatbed scanner but required for feeder
                        if (documentHandlingSelect != null)
                        {
                            // check for document feeder
                            if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) &amp;amp;amp;amp; WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
                            {
                                hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) &amp;amp;amp;amp; WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
                            }
                        }
                    }
                }
    
                return images;
            }
    
            /// <summary>
            /// Gets the list of available WIA devices.
            /// </summary>
            /// <returns></returns>
            public static List<string> GetDevices()
            {
                List<string> devices = new List<string>();
                WIA.DeviceManager manager = new WIA.DeviceManager();
    
                foreach (WIA.DeviceInfo info in manager.DeviceInfos)
                {
                    devices.Add(info.DeviceID);
                }
    
                return devices;
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.