private void DoTransfer(ScannedImageSource.Concrete source, WiaDevice device, WiaItem item) { if (ScanProfile.PaperSource != ScanSource.Glass && !device.SupportsFeeder()) { throw new NoFeederSupportException(); } if (ScanProfile.PaperSource == ScanSource.Duplex && !device.SupportsDuplex()) { throw new NoDuplexSupportException(); } InitProgress(device); ConfigureProps(device, item); using (var transfer = item.StartTransfer()) { int pageNumber = 1; transfer.PageScanned += (sender, args) => { try { using (args.Stream) using (var output = Image.FromStream(args.Stream)) { ProduceImage(source, output, ref pageNumber); } } catch (Exception e) { ScanException = e; } }; transfer.Progress += (sender, args) => smoothProgress.InputProgressChanged(args.Percent / 100.0); using (CancelToken.Register(transfer.Cancel)) { transfer.Download(); if (device.Version == WiaVersion.Wia10 && ScanProfile.PaperSource != ScanSource.Glass) { // For WIA 1.0 feeder scans, we need to repeatedly call Download until WIA_ERROR_PAPER_EMPTY is received. try { while (!CancelToken.IsCancellationRequested) { transfer.Download(); } } catch (WiaException e) when(e.ErrorCode == WiaErrorCodes.PAPER_EMPTY) { } } } } }
private void ConfigureProps(WiaDevice device, WiaItem item) { if (ScanProfile.UseNativeUI) { return; } if (ScanProfile.PaperSource != ScanSource.Glass) { if (device.Version == WiaVersion.Wia10) { device.SetProperty(WiaPropertyId.DPS_PAGES, 1); } else { item.SetProperty(WiaPropertyId.IPS_PAGES, 0); } } if (device.Version == WiaVersion.Wia10) { switch (ScanProfile.PaperSource) { case ScanSource.Glass: device.SetProperty(WiaPropertyId.DPS_DOCUMENT_HANDLING_SELECT, WiaPropertyValue.FLATBED); break; case ScanSource.Feeder: device.SetProperty(WiaPropertyId.DPS_DOCUMENT_HANDLING_SELECT, WiaPropertyValue.FEEDER); break; case ScanSource.Duplex: device.SetProperty(WiaPropertyId.DPS_DOCUMENT_HANDLING_SELECT, WiaPropertyValue.FEEDER | WiaPropertyValue.DUPLEX); break; } } else { switch (ScanProfile.PaperSource) { case ScanSource.Feeder: item.SetProperty(WiaPropertyId.IPS_DOCUMENT_HANDLING_SELECT, WiaPropertyValue.FRONT_ONLY); break; case ScanSource.Duplex: item.SetProperty(WiaPropertyId.IPS_DOCUMENT_HANDLING_SELECT, WiaPropertyValue.DUPLEX); break; } } switch (ScanProfile.BitDepth) { case ScanBitDepth.Grayscale: item.SetProperty(WiaPropertyId.IPA_DATATYPE, 2); break; case ScanBitDepth.C24Bit: item.SetProperty(WiaPropertyId.IPA_DATATYPE, 3); break; case ScanBitDepth.BlackWhite: item.SetProperty(WiaPropertyId.IPA_DATATYPE, 0); break; } int xRes = ScanProfile.Resolution.ToIntDpi(); int yRes = xRes; item.SetPropertyClosest(WiaPropertyId.IPS_XRES, ref xRes); item.SetPropertyClosest(WiaPropertyId.IPS_YRES, ref yRes); PageDimensions pageDimensions = ScanProfile.PageSize.PageDimensions() ?? ScanProfile.CustomPageSize; if (pageDimensions == null) { throw new InvalidOperationException("No page size specified"); } int pageWidth = pageDimensions.WidthInThousandthsOfAnInch() * xRes / 1000; int pageHeight = pageDimensions.HeightInThousandthsOfAnInch() * yRes / 1000; int horizontalSize, verticalSize; if (device.Version == WiaVersion.Wia10) { horizontalSize = (int)device.Properties[ScanProfile.PaperSource == ScanSource.Glass ? WiaPropertyId.DPS_HORIZONTAL_BED_SIZE : WiaPropertyId.DPS_HORIZONTAL_SHEET_FEED_SIZE].Value; verticalSize = (int)device.Properties[ScanProfile.PaperSource == ScanSource.Glass ? WiaPropertyId.DPS_VERTICAL_BED_SIZE : WiaPropertyId.DPS_VERTICAL_SHEET_FEED_SIZE].Value; } else { horizontalSize = (int)item.Properties[WiaPropertyId.IPS_MAX_HORIZONTAL_SIZE].Value; verticalSize = (int)item.Properties[WiaPropertyId.IPS_MAX_VERTICAL_SIZE].Value; } int pagemaxwidth = horizontalSize * xRes / 1000; int pagemaxheight = verticalSize * yRes / 1000; int horizontalPos = 0; if (ScanProfile.PageAlign == ScanHorizontalAlign.Center) { horizontalPos = (pagemaxwidth - pageWidth) / 2; } else if (ScanProfile.PageAlign == ScanHorizontalAlign.Left) { horizontalPos = (pagemaxwidth - pageWidth); } pageWidth = pageWidth < pagemaxwidth ? pageWidth : pagemaxwidth; pageHeight = pageHeight < pagemaxheight ? pageHeight : pagemaxheight; if (ScanProfile.WiaOffsetWidth) { item.SetProperty(WiaPropertyId.IPS_XEXTENT, pageWidth + horizontalPos); item.SetProperty(WiaPropertyId.IPS_XPOS, horizontalPos); } else { item.SetProperty(WiaPropertyId.IPS_XEXTENT, pageWidth); item.SetProperty(WiaPropertyId.IPS_XPOS, horizontalPos); } item.SetProperty(WiaPropertyId.IPS_YEXTENT, pageHeight); if (!ScanProfile.BrightnessContrastAfterScan) { item.SetPropertyRange(WiaPropertyId.IPS_CONTRAST, ScanProfile.Contrast, -1000, 1000); item.SetPropertyRange(WiaPropertyId.IPS_BRIGHTNESS, ScanProfile.Brightness, -1000, 1000); } }