示例#1
0
        private static void SaveImageToJpgFile(ImageFile image, string fileName)
        {
            WIA.ImageProcess imgProcess      = new WIA.ImageProcess();
            object           convertFilter   = "Convert";
            string           convertFilterID = imgProcess.FilterInfos.get_Item(ref convertFilter).FilterID;

            imgProcess.Filters.Add(convertFilterID, 0);
            SetWIAProperty(imgProcess.Filters[imgProcess.Filters.Count].Properties, "FormatID", WIA.FormatID.wiaFormatJPEG);
            image = imgProcess.Apply(image);
            image.SaveFile(fileName);
        }
示例#2
0
        private void button1_Click(object sender, EventArgs e)
        {
            /*DeviceManager deviceManager = new DeviceManagerClass();
             * for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
             * {
             *      if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) continue;
             *      textBox1.AppendText(String.Format("{0} = {1}", i, deviceManager.DeviceInfos[i].Properties["Name"].get_Value()));
             * }
             *
             * DeviceInfo deviceInfo = deviceManager.DeviceInfos[1];*/
            DeviceInfo deviceInfo = deviceManager.DeviceInfos[cmbScanners.SelectedIndex + 1];
            Device     device     = deviceInfo.Connect();
            Item       scanner    = device.Items[1];

            int resolution   = int.Parse(cmbResolution.SelectedItem.ToString());
            int width_pixel  = (int)(A4width * resolution);
            int height_pixel = (int)(A4height * resolution);


            //#define WIA_IPA_DATATYPE                                     4103 // 0x1007
            //#define WIA_IPA_DATATYPE_STR                                 L"Data Type"
            //#define WIA_DATA_THRESHOLD             0
            //#define WIA_DATA_DITHER                1
            //#define WIA_DATA_GRAYSCALE             2
            //#define WIA_DATA_COLOR                 3
            //#define WIA_DATA_COLOR_THRESHOLD       4
            //#define WIA_DATA_COLOR_DITHER          5

            //
            // WIA_IPS_CUR_INTENT flags
            // WIA_IPS_CUR_INTENT
            //#define WIA_INTENT_NONE                   0x00000000
            //#define WIA_INTENT_IMAGE_TYPE_COLOR       0x00000001
            //#define WIA_INTENT_IMAGE_TYPE_GRAYSCALE   0x00000002
            //#define WIA_INTENT_IMAGE_TYPE_TEXT        0x00000004
            //#define WIA_INTENT_IMAGE_TYPE_MASK        0x0000000F
            //#define WIA_INTENT_MINIMIZE_SIZE          0x00010000
            //#define WIA_INTENT_MAXIMIZE_QUALITY       0x00020000
            //#define WIA_INTENT_BEST_PREVIEW           0x00040000
            //#define WIA_INTENT_SIZE_MASK              0x000F0000

            //#define WIA_IPA_DEPTH                                        4104 // 0x1008
            //#define WIA_IPA_DEPTH_STR                                    L"Bits Per Pixel"

            int WIA_IPS_CUR_INTENT = 0;           // WIA_INTENT_NONE
            int WIA_IPA_DATATYPE   = 0;           // WIA_DATA_THRESHOLD
            int WIA_IPA_DEPTH      = 0;

            switch (cmbColor.SelectedIndex)
            {
            case 0:                     // ч-б
                WIA_IPA_DATATYPE   = 0; // WIA_DATA_THRESHOLD
                WIA_IPS_CUR_INTENT = 4; // WIA_INTENT_IMAGE_TYPE_TEXT
                WIA_IPA_DEPTH      = 1;
                break;

            case 1:                     // серый
                WIA_IPA_DATATYPE   = 2; // WIA_DATA_GRAYSCALE
                WIA_IPS_CUR_INTENT = 2; // WIA_INTENT_IMAGE_TYPE_GRAYSCALE
                WIA_IPA_DEPTH      = 8;
                break;

            case 2:                     // цвет
                WIA_IPA_DATATYPE   = 3; // WIA_DATA_COLOR
                WIA_IPS_CUR_INTENT = 1; // WIA_INTENT_IMAGE_TYPE_COLOR
                WIA_IPA_DEPTH      = 24;
                break;
            }

            //Guid WiaImgFmt_JPEG = new Guid(0xb96b3cae, 0x0728, 0x11d3, 0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e);
            //scanner.Properties["4106"].set_Value(WiaImgFmt_JPEG);

            //#define WIA_IPA_COMPRESSION                                  4107 // 0x100b
            //#define WIA_IPA_COMPRESSION_STR                              L"Compression"
            //
            // WIA_IPA_COMPRESSION constants
            //
            //#define WIA_COMPRESSION_NONE           0
            //#define WIA_COMPRESSION_BI_RLE4        1
            //#define WIA_COMPRESSION_BI_RLE8        2
            //#define WIA_COMPRESSION_G3             3
            //#define WIA_COMPRESSION_G4             4
            //#define WIA_COMPRESSION_JPEG           5

            /*for (int i = 0; i <= 5; i++)
             * {
             *      System.Diagnostics.Debug.WriteLine(i);
             *      try
             *      {
             *              scanner.Properties["4107"].set_Value(i);
             *      }
             *      catch (ArgumentException comerr)
             *      {
             *              System.Diagnostics.Debug.WriteLine(comerr.Message);
             *      }
             * }*/

            //
            // WIA_DPS_DOCUMENT_HANDLING_SELECT flags
            //
            //#define FEEDER                        0x001
            //#define FLATBED                       0x002
            //#define DUPLEX                        0x004
            //#define FRONT_FIRST                   0x008
            //#define BACK_FIRST                    0x010
            //#define FRONT_ONLY                    0x020
            //#define BACK_ONLY                     0x040
            //#define NEXT_PAGE                     0x080
            //#define PREFEED                       0x100
            //#define AUTO_ADVANCE                  0x200

            //device.Properties["Document Handling Select"].set_Value(5); // 3088
            int duplex_mode = 2;             // Планшет

            //device.Properties["3096"].set_Value(1); // WIA_IPS_PAGES Планшет - 1 страница

            if (chkFeeder.Checked)
            {
                //device.Properties["3096"].set_Value(0); // Много страниц
                if (chkDuplex.Checked)
                {
                    duplex_mode = 5;                     // Автоподатчик обе стороны
                }
                else
                {
                    duplex_mode = 1;                     // Автоподатчик одна сторона
                }
            }

            device.Properties["Document Handling Select"].set_Value(duplex_mode);

            // Яркость WIA_IPS_BRIGHTNESS 6154
            scanner.Properties["6154"].set_Value(trkBrightness.Value);
            // Контраст WIA_IPS_CONTRAST 6155
            scanner.Properties["6155"].set_Value(trkContrast.Value);

            scanner.Properties["4103"].set_Value(WIA_IPA_DATATYPE);
            scanner.Properties["4104"].set_Value(WIA_IPA_DEPTH);
            scanner.Properties["6146"].set_Value(WIA_IPS_CUR_INTENT);

            scanner.Properties["6147"].set_Value(resolution);
            scanner.Properties["6148"].set_Value(resolution);

            scanner.Properties["6151"].set_Value(width_pixel);
            scanner.Properties["6152"].set_Value(height_pixel);

            //Type type = scanner.Properties["4107"].GetType;
            //System.Diagnostics.Debug.WriteLine("4107: " + type.);

            /*foreach (Property propertyItem in scanner.Properties)
             * {
             *      if (!propertyItem.IsReadOnly)
             *      {
             *              System.Diagnostics.Debug.WriteLine(String.Format("{0}\t{1}\t{2}", propertyItem.Name, propertyItem.PropertyID, propertyItem.get_Value()));
             *      }
             * }*/

            CommonDialogClass commonDialog = new CommonDialogClass();

            WIA.ImageProcess imageProcess = new WIA.ImageProcess();              // use to compress jpeg.
            imageProcess.Filters.Add(imageProcess.FilterInfos["Convert"].FilterID);
            imageProcess.Filters[1].Properties["FormatID"].set_Value(FormatID.wiaFormatJPEG);
            imageProcess.Filters[1].Properties["Quality"].set_Value(trkJpegQuality.Value * 10);

            ImageFile imageFileFront;
            ImageFile imageFileBack;

            try
            {
                while (true)
                {
                    imageFileFront = (ImageFile)commonDialog.ShowTransfer(scanner, FormatID.wiaFormatJPEG, true);
                    //imageFileFront = (ImageFile)scanner.Transfer(FormatID.wiaFormatJPEG);
                    filePathFront = GetFileName();                     // Path.GetTempFileName() + ".jpg";
                    imageProcess.Apply(imageFileFront).SaveFile(filePathFront);
                    //imageFileFront.SaveFile(filePathFront);
                    Marshal.ReleaseComObject(imageFileFront);

                    /*thumb = Image.FromFile(filePathFront).GetThumbnailImage(105, 149, () => false, IntPtr.Zero);
                     *
                     * imageList.Images.Add(thumb);
                     * listView1.Items.Add(Path.GetFileName(filePathFront));
                     * listView1.Items[count].ImageIndex = count++;
                     *
                     * scanFileNames.Add(filePathFront);*/
                    AppendFile(filePathFront);

                    if (!chkFeeder.Checked)                     // Планшет - сканируем только одну страницу
                    {
                        break;
                    }

                    if (chkDuplex.Checked)
                    {
                        imageFileBack = (ImageFile)commonDialog.ShowTransfer(scanner, FormatID.wiaFormatJPEG, true);
                        //imageFileBack = (ImageFile)scanner.Transfer(FormatID.wiaFormatJPEG);
                        filePathBack = GetFileName();                         // Path.GetTempFileName() + ".jpg";
                        imageProcess.Apply(imageFileBack).SaveFile(filePathBack);
                        //imageFileBack.SaveFile(filePathBack);
                        Marshal.ReleaseComObject(imageFileBack);

                        /*thumb = Image.FromFile(filePathBack).GetThumbnailImage(105, 149, () => false, IntPtr.Zero);
                         *
                         * imageList.Images.Add(thumb);
                         * listView1.Items.Add(Path.GetFileName(filePathBack));
                         * listView1.Items[count].ImageIndex = count++;
                         *
                         * scanFileNames.Add(filePathBack);*/
                        AppendFile(filePathBack);
                    }
                }
            }
            catch (COMException comerr)
            {
                switch ((uint)comerr.ErrorCode)
                {
                case 0x80210003:
                    toolStripStatus.Text = "Страницы закончились";
                    break;

                case 0x80210006:
                    toolStripStatus.Text = "Сканер занят или не готов";
                    break;

                case 0x80210064:
                    toolStripStatus.Text = "Сканирование отменено";
                    break;

                case 0x8021000C:
                    toolStripStatus.Text = "Некорректные настройки устройства WIA";
                    break;

                case 0x80210005:
                    toolStripStatus.Text = "Устройство выключено";
                    break;

                case 0x80210001:
                    toolStripStatus.Text = "Неведомая ошибка";
                    break;
                }
            }
            finally
            {
                //imageFileFront = null;
                //imageFileBack = null;
                //thumb = null;
            }
        }