WIA将图像扫描为双面图像并使用进纸器选项

时间:2018-07-20 12:23:18

标签: c# asp.net scanning twain wia

我正在一个必须与扫描仪进行通信的项目中。我将有一页文件以及双页文件。我要扫描的是文档的一页。这是我的代码

private void button1_Click(object sender, EventArgs e)
{
   CommonDialogClass commonDialogClass=new CommonDialogClass();
            Device scannerDevice = commonDialogClass.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, false, false);
            string fileName = "";
            if (scannerDevice != null)
            {
                Item scannerItem = scannerDevice.Items[1];
                AdjustScannerSettings(scannerItem,150,0,0,1250,1700,0,0,1);
                object scanResult = commonDialogClass.ShowTransfer(scannerItem, WIA.FormatID.wiaFormatPNG, false);
                if (scanResult != null)
                {
                    ImageFile image = (ImageFile) scanResult;

                    fileName = Directory.GetCurrentDirectory() +"\\"+DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss-fffffff");
                    SaveImageToPNGFile(image, fileName+".png");
                    pictureBoxScannedImage.ImageLocation = fileName;
                }
            }
            PngToPdf(fileName);
        }

        private void PngToPdf(string fileName)
        {
            Document document = new Document();
            using (var stream = new FileStream((fileName + ".pdf"), FileMode.Create, FileAccess.Write, FileShare.None))
            {
                PdfWriter.GetInstance(document, stream);

                using (var imageStream = new FileStream((fileName + ".png"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var image = iTextSharp.text.Image.GetInstance(imageStream);
                    document.SetPageSize(new iTextSharp.text.Rectangle(0, 0, image.Width, image.Height, 0));
                    document.Open();
                    document.Add(image);
                }

                document.Close();
            }
        }

        private void SaveImageToPNGFile(ImageFile image, string fileName)
        {
            ImageProcess imgProcess=new 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.wiaFormatPNG);
            image = imgProcess.Apply(image);
            image.SaveFile(fileName);

        }

        //https://support.microsoft.com/en-us/help/2709992/wia-automation-library-2-0-does-not-support-duplex-scan

        private static void AdjustScannerSettings(IItem scannnerItem, int scanResolutionDPI, int scanStartLeftPixel, int scanStartTopPixel,
            int scanWidthPixels, int scanHeightPixels, int brightnessPercents, int contrastPercents,int colorMode)
        {
            const string WIA_HORIZONTAL_SCAN_RESOLUTION_DPI = "6147";
            const string WIA_VERTICAL_SCAN_RESOLUTION_DPI = "6148";
            const string WIA_HORIZONTAL_SCAN_START_PIXEL = "6149";
            const string WIA_VERTICAL_SCAN_START_PIXEL = "6150";
            const string WIA_HORIZONTAL_SCAN_SIZE_PIXELS = "6151";
            const string WIA_VERTICAL_SCAN_SIZE_PIXELS = "6152";
            const string WIA_SCAN_BRIGHTNESS_PERCENTS = "6154";
            const string WIA_SCAN_CONTRAST_PERCENTS = "6155";
            const string WIA_SCAN_COLOR_MODE = "6146";
            SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
            SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
            SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_START_PIXEL, scanStartLeftPixel);
            SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_START_PIXEL, scanStartTopPixel);
            SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_SIZE_PIXELS, scanWidthPixels);
            SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_SIZE_PIXELS, scanHeightPixels);
            SetWIAProperty(scannnerItem.Properties, WIA_SCAN_BRIGHTNESS_PERCENTS, brightnessPercents);
            SetWIAProperty(scannnerItem.Properties, WIA_SCAN_CONTRAST_PERCENTS, contrastPercents);
            SetWIAProperty(scannnerItem.Properties, WIA_SCAN_COLOR_MODE, colorMode);
        }

        private static void SetWIAProperty(IProperties properties, object propName, object propValue)
        {
            Property prop = properties.get_Item(ref propName);
            prop.set_Value(ref propValue);
        }

我搜索并发现

const uint WIA_ADF_DUPLEX = 0x005;
const string WIA_DPS_DOCUMENT_HANDLING_SELECT = "3088";
应该在代码中设置

值,但是,我无法处理这两个变量。

关于向扫描仪提供多个文档,由于我没有处理此情况,因此代码会到达最后一页。如果有人给我一些提示或对此的实现,我会很高兴。

另一个问题,我使用的是Dynamsoft Web twain sdk,并且运行良好,现在我还需要使用twain实施同一项目,而不使用dynamsoft sdk。是否有任何教程解释与吐温相同的过程。

0 个答案:

没有答案