Exemplo n.º 1
0
        private void Scan(object sender, RoutedEventArgs e)
        {
            try
            {
                AppBarButton button = sender as AppBarButton;
                // hmmm, reusing the device doesn't work because it somehow resets itself to capture higher resolution images.
                bool hasDevice = false; //  device != null;

                GetScannerAsync(new Action(() =>
                {
                    WIA.ImageFile imageFile = null;

                    if (!hasDevice)
                    {
                        imageFile = globalDialog.ShowAcquireImage(DeviceType: WIA.WiaDeviceType.ScannerDeviceType,
                                                                  Bias: WIA.WiaImageBias.MinimizeSize, Intent: WIA.WiaImageIntent.TextIntent, AlwaysSelectDevice: false);
                    }
                    else
                    {
                        WIA.Item scannerItem      = device.Items[1];
                        const string wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
                        object scanResult         = globalDialog.ShowTransfer(scannerItem, wiaFormatPNG, false);
                        imageFile = (WIA.ImageFile)scanResult;
                    }

                    if (imageFile != null)
                    {
                        string temp = System.IO.Path.GetTempFileName();
                        if (File.Exists(temp))
                        {
                            File.Delete(temp);
                        }
                        imageFile.SaveFile(temp);
                        TempFilesManager.AddTempFile(temp);

                        AttachmentDialogImageItem image = new AttachmentDialogImageItem(temp);
                        AddItem(image);
                        LayoutContent();
                        SelectItem(image);
                        AutoCrop(image);
                    }
                }));
            }
            catch (Exception ex)
            {
                device = null;
                string message = GetWiaErrorMessage(ex);
                MessageBox.Show(this, message, "Scan Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        void scan(string scannerId)
        {
            scanner = null;
            foreach (WIA.DeviceInfo devInfo in manager.DeviceInfos)
            {
                if (devInfo.DeviceID == scannerId)
                {
                    scanner = devInfo.Connect();
                }
            }
            if (scanner == null)
            {
                throw new Exception("Selected scanner is unavailable!");
            }

            WIA.Item item = scanner.Items[1];

            try
            {
                commonDialog = null;
                commonDialog = new WIA.CommonDialog();
                scanInit();
                scannedImage = (WIA.ImageFile)commonDialog.ShowTransfer(item, wiaFormatBMP, false);
                imageBytes   = (byte[])scannedImage.FileData.get_BinaryData();
                MemoryStream ms = new MemoryStream(imageBytes);
                image = Image.FromStream(ms);
                pbScannedImage.Image = image;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                Marshal.ReleaseComObject(scannedImage);
            }
        }