Created
September 30, 2018 01:01
-
-
Save abfo/b7222997791abcf731518fbdf470eebf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private XImage ScanOne() | |
{ | |
XImage ximage = null; | |
try | |
{ | |
// find our device (scanner previously selected with commonDialog.ShowSelectDevice) | |
DeviceManager manager = new DeviceManager(); | |
DeviceInfo deviceInfo = null; | |
foreach (DeviceInfo info in manager.DeviceInfos) | |
{ | |
if (info.DeviceID == _deviceId) | |
{ | |
deviceInfo = info; | |
} | |
} | |
if (deviceInfo != null) | |
{ | |
Device device = deviceInfo.Connect(); | |
CommonDialog commonDialog = new CommonDialog(); | |
Item item = device.Items[1]; | |
int dpi = 150; | |
// configure item | |
SetItemIntProperty(ref item, 6146, 2); // greyscale | |
SetItemIntProperty(ref item, 6147, dpi); // 150 dpi | |
SetItemIntProperty(ref item, 6148, dpi); // 150 dpi | |
SetItemIntProperty(ref item, 6151, (int)(dpi * _width)); // scan width | |
SetItemIntProperty(ref item, 6152, (int)(dpi * _height)); // scan height | |
SetItemIntProperty(ref item, 4104, 8); // bit depth | |
int deviceHandling = _adf ? 1 : 2; // 1 for ADF, 2 for flatbed | |
// configure device | |
SetDeviceIntProperty(ref device, 3088, deviceHandling); | |
int handlingStatus = GetDeviceIntProperty(ref device, 3087); | |
if (handlingStatus == deviceHandling) | |
{ | |
ImageFile image = commonDialog.ShowTransfer(item, formatJpeg, true); | |
// save image to a temp file and then load into an XImage | |
string tempPath = System.IO.Path.GetTempFileName(); | |
File.Delete(tempPath); | |
tempPath = System.IO.Path.ChangeExtension(tempPath, "jpg"); | |
image.SaveFile(tempPath); | |
ximage = XImage.FromFile(tempPath); | |
_tempFilesToDelete.Add(tempPath); | |
} | |
} | |
} | |
catch (COMException ex) | |
{ | |
ximage = null; | |
// paper empty | |
if ((uint)ex.ErrorCode != 0x80210003) | |
{ | |
throw; | |
} | |
} | |
return ximage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment