0

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;
    }

スキャンが完了すると、「動的操作は同種の AppDomain でのみ実行できます。」という例外が発生します。誰かが私が間違っていることを説明できますか? 別のスレッドで使用しようとしましたが、それでも同じ例外が発生します。

スキャナ: DSmobile 700D。

4

0 に答える 0