// Return the lowest supported bitmap that is at least the target bitmap private TwainResolution FindBestResolution(TwainResolution[] resolutions, int targetResolution) { float diff = int.MaxValue; TwainResolution selectedResolution = null; foreach (TwainResolution resolution in resolutions) { if (resolution.X >= targetResolution && (resolution.X - targetResolution) < diff) { diff = resolution.X - targetResolution; selectedResolution = resolution; } } return(selectedResolution); }
private void ScanDocument() { this.Cursor = Cursors.WaitCursor; if (device.TryOpen()) { try { DocumentType docType = ((myDocType)getCheckedMenuItem(miDocumentType).Tag).DocType; DeviceCapability[] capabilities = device.GetSupportedCapabilities(); foreach (DeviceCapability capability in capabilities) { switch (capability) { case DeviceCapability.ICAP_PIXELTYPE: if (docType.PreferredColorDepth != Arena.Enums.ColorDepth.Undefined) { ImagePixelType[] pixelTypes = device.GetSupportedPixelTypes(); switch (docType.PreferredColorDepth) { case Arena.Enums.ColorDepth.Color_4_bit: case Arena.Enums.ColorDepth.Color_8_bit: case Arena.Enums.ColorDepth.Color_24_bit: case Arena.Enums.ColorDepth.Color_32_bit: case Arena.Enums.ColorDepth.Color_48_bit: foreach (ImagePixelType pixelType in pixelTypes) { if (pixelType == ImagePixelType.Color) { device.PixelType = pixelType; break; } } break; case Arena.Enums.ColorDepth.Grayscale_8_bit: case Arena.Enums.ColorDepth.Grayscale_10_bit: case Arena.Enums.ColorDepth.Grayscale_24_bit: foreach (ImagePixelType pixelType in pixelTypes) { if (pixelType == ImagePixelType.Grayscale) { device.PixelType = pixelType; break; } } break; case Arena.Enums.ColorDepth.Black_White: foreach (ImagePixelType pixelType in pixelTypes) { if (pixelType == ImagePixelType.BlackAndWhite) { device.PixelType = pixelType; break; } } break; } } break; case DeviceCapability.ICAP_BITDEPTH: if (docType.PreferredColorDepth != Arena.Enums.ColorDepth.Undefined) { int[] bitDepths = device.GetSupportedBitDepths(); int selectedBithDepth = 0; switch (docType.PreferredColorDepth) { case Arena.Enums.ColorDepth.Black_White: selectedBithDepth = FindBestBitDepth(bitDepths, 1); break; case Arena.Enums.ColorDepth.Color_4_bit: selectedBithDepth = FindBestBitDepth(bitDepths, 4); break; case Arena.Enums.ColorDepth.Color_8_bit: selectedBithDepth = FindBestBitDepth(bitDepths, 8); break; case Arena.Enums.ColorDepth.Color_24_bit: selectedBithDepth = FindBestBitDepth(bitDepths, 24); break; case Arena.Enums.ColorDepth.Color_32_bit: selectedBithDepth = FindBestBitDepth(bitDepths, 32); break; case Arena.Enums.ColorDepth.Color_48_bit: selectedBithDepth = FindBestBitDepth(bitDepths, 48); break; case Arena.Enums.ColorDepth.Grayscale_8_bit: selectedBithDepth = FindBestBitDepth(bitDepths, 8); break; case Arena.Enums.ColorDepth.Grayscale_10_bit: selectedBithDepth = FindBestBitDepth(bitDepths, 10); break; case Arena.Enums.ColorDepth.Grayscale_24_bit: selectedBithDepth = FindBestBitDepth(bitDepths, 24); break; } if (selectedBithDepth > 0) { device.BitDepth = selectedBithDepth; } } break; case DeviceCapability.ICAP_XRESOLUTION: if (docType.PreferredResolution != Resolution.Undefined) { TwainResolution[] resolutions = device.GetSupportedResolutions(); TwainResolution selectedResolution = null; switch (docType.PreferredResolution) { case Resolution.DPI_72: selectedResolution = FindBestResolution(resolutions, 72); break; case Resolution.DPI_150: selectedResolution = FindBestResolution(resolutions, 150); break; case Resolution.DPI_300: selectedResolution = FindBestResolution(resolutions, 300); break; case Resolution.DPI_600: selectedResolution = FindBestResolution(resolutions, 600); break; } if (selectedResolution != null) { device.Resolution = selectedResolution; } } break; } } switch (getCheckedMenuItemIndex(miPageSize)) { case 0: device.FrameSize = StaticFrameSizeType.LetterUS; break; case 1: device.FrameSize = StaticFrameSizeType.LegalUS; break; case 2: device.FrameSize = StaticFrameSizeType.USStatement; break; } device.AutomaticDeskew = true; device.AutoScan = true; device.HideInterface = true; device.DisplayProgressIndicator = false; device.Acquire(); } catch (Exception ex) { this.Cursor = Cursors.Default; device.Close(); MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } } this.Cursor = Cursors.Default; }