Skip to content

Instantly share code, notes, and snippets.

@abfo
Created September 30, 2018 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abfo/b7222997791abcf731518fbdf470eebf to your computer and use it in GitHub Desktop.
Save abfo/b7222997791abcf731518fbdf470eebf to your computer and use it in GitHub Desktop.
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