Exemple #1
0
        private static WiaResult ShowWiaErrorCode(COMException ex)
        {
            WiaResult error = new WiaResult();

            if (Enum.IsDefined(typeof(WiaError), (uint)ex.ErrorCode))
            {
                error.Error = ((WiaError)ex.ErrorCode);
            }
            else
            {
                error.Error = WiaError.UNKNOWN;
            }
            error.Message = ex.Message;
            return(error);
        }
Exemple #2
0
        /// <summary>
        /// Scan images.
        /// </summary>
        /// <param name="images">Result image file path.</param>
        /// <param name="scannerId">Guid of selected scanner.</param>
        /// <param name="settings"></param>
        public WiaResult Scan(out List <string> images, string scannerId, ScanSettings settings)
        {
            // give a default setting if null
            if (settings == null)
            {
                settings = new ScanSettings();
            }
            images = new List <string>();
            // parse file extension
            string extension;

            switch (settings.ImageFormat)
            {
            default:
            case WiaImageFormat.PNG:
                extension = ".png";
                break;

            case WiaImageFormat.BMP:
                extension = ".bmp";
                break;

            case WiaImageFormat.GIF:
                extension = ".gif";
                break;

            case WiaImageFormat.JPEG:
                extension = ".jpg";
                break;

            case WiaImageFormat.TIFF:
                extension = ".tif";
                break;
            }
            // prepare result
            WiaResult result = new WiaResult();

            result.Error = WiaError.SUCCESS;
            bool hasMorePages = true;

            while (hasMorePages)
            {
                // select the correct scanner using the provided scannerId parameter
                Device device = null;
                foreach (DeviceInfo info in _manager.DeviceInfos)
                {
                    if (info.DeviceID == scannerId)
                    {
                        // connect to scanner
                        device = info.Connect();
                        Property nameProp         = info.Properties["Name"];
                        Property typeProp         = info.Properties["Manufacturer"];
                        Property manufacturerProp = info.Properties["Manufacturer"];
                        Console.WriteLine(nameProp.get_Value().ToString());
                        Console.WriteLine(typeProp.get_Value().ToString());
                        Console.WriteLine(manufacturerProp.get_Value().ToString());
                        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
                    string    message = "The device with provided ID could not be found. Available Devices:\n" + availableDevices;
                    WiaResult error   = new WiaResult {
                        Error = WiaError.DEVICE_NOT_FOUND, Message = message
                    };
                    return(error);
                }

                // check paper
                hasMorePages = HasMorePages(device);
                if (!hasMorePages)
                {
                    break;
                }
                // scan
                Item item = device.Items[1];
                try
                {
                    SetScanSettings(item, settings);
                    ImageFile image;
                    if (settings.ShowUI)
                    {
                        // scan image with dialog
                        ICommonDialog wiaCommonDialog = new CommonDialog();
                        image = (ImageFile)wiaCommonDialog.ShowTransfer(item, settings.ImageFormat, false);
                    }
                    else
                    {
                        image = (ImageFile)item.Transfer(settings.ImageFormat);
                    }

                    // create temp directory

                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    // save to temp file
                    string fileName = path + DateTime.Now.ToString("yyyyMMdd-HHmmss", System.Globalization.CultureInfo.InvariantCulture) + extension;
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }
                    image.SaveFile(fileName);
                    // add file to output list
                    images.Add(fileName);
                }
                catch (COMException ex)
                {
                    result = ShowWiaErrorCode(ex);
                    // if there are successfull scans, we juse break without showing error
                    if (result.Error == WiaError.WIA_ERROR_PAPER_EMPTY && images.Count > 0)
                    {
                        result.Error = WiaError.SUCCESS;
                    }
                    break;
                }
                catch (Exception ex)
                {
                    result.Error   = WiaError.UNKNOWN;
                    result.Message = ex.Message;
                    break;
                }
                finally
                {
                    hasMorePages = HasMorePages(device);
                }
            }
            return(result);
        }