0

I have a problem with scanning with WIA.

public List<Image> Scan(string scannerId)
    {
        var images = new List<Image>();

        var hasMorePages = true;
        while (hasMorePages)
        {
            // select the correct scanner using the provided scannerId parameter
            var manager = new DeviceManager();
            Device device = null;
            foreach (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 (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);
            }

            Item item = device.Items[1];

            try
            {
                // scan image
                ICommonDialog wiaCommonDialog = new CommonDialog();
                var image = (ImageFile) wiaCommonDialog.ShowTransfer(item, WiaFormatBmp, true); // <--- exception goes from there

                // save to temp file
                string fileName = "test.bmp";//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
                Property documentHandlingSelect = null;
                Property documentHandlingStatus = null;

                foreach (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()) && WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
                    {
                        hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) &&
                                         WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
                    }
                }
            }
        }

        return images;
    }

When the scanning is completed i get an exception: "Dynamic operations can only be performed in homogenous AppDomain.". Can someone explain me what im doing wrong? I tried to use it in another Thread but still get the same exception.

Scanner: DSmobile 700D.

Paweł Marecki
  • 630
  • 1
  • 9
  • 21
  • This exception is strongly associated with a .config file. Make sure you don't use the legacyCasPolicy element, relevant in a .NET 4+ app. – Hans Passant Jun 05 '13 at 14:48
  • @HansPassant Thanks for your advice. I just use ``. Here is my full cfg (need to run .NET 1.1 dll on net 4.0 project): ` ` Tomorrow I'll check it and let know how it works. – Paweł Marecki Jun 05 '13 at 16:28
  • You'll get to choose between keeping that .NET 1.1 code running, downgrading to .NET 3.5 or using WIA. Nice to have choices, can't make them for you ;) – Hans Passant Jun 05 '13 at 16:36
  • So there is no other way to keep these three things together in one project? I can't build code from question on .NET 3.5. F.g there is no Image.SaveFile method :(. – Paweł Marecki Jun 05 '13 at 17:25
  • I just build console application for scanning module. Thank you very much Hans Passant you make my work easier ;). – Paweł Marecki Jun 06 '13 at 11:24

0 Answers0