I am following your example for scanning the top part of a page. I am testing on 2 scanners. One is a Canon and the other is a Fujitsu Fi-7160. It works fine on the Canon, but the Fujitsu scans the entire page every time. Here is the code that I believe is meaningful:
Code: Select all
device.UnitOfMeasure = UnitOfMeasure.Inches;
device.ImageLayout.Reset();
device.ImageLayout.Set(0f, 0f, 8.5f, 5f);
device.Acquire();
Here is the full code:
Code: Select all
class Program
{
/// <summary>
/// The main program.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.WriteLine("Starting...");
TwainGlobalSettings.Register("S******", "****@******.com", "****-**-**", "*****");
try
{
Console.WriteLine("Select a scanner by entering its number. Press x to quit.");
DeviceManager deviceManager = new DeviceManager();
deviceManager.Open();
Scan(deviceManager);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.ReadLine();
}
}
/// <summary>
/// Scans an image.
/// </summary>
/// <param name="deviceManager"></param>
private static void Scan(DeviceManager deviceManager)
{
Device device = null;
try
{
ListDevices(deviceManager);
var selected = GetSelectedScanner(deviceManager);
if (selected >= 0)
{
device = deviceManager.Devices[selected];
device.Open();
device.ShowUI = false;
SubscribeToDeviceEvents(device);
device.UnitOfMeasure = UnitOfMeasure.Inches;
device.ImageLayout.Reset();
device.ImageLayout.Set(0f, 0f, 8.5f, 5f);
device.Acquire();
}
}
catch
{
// if device is found
if (device != null)
{
// if device is opened
if (device.State >= DeviceState.Opened)
{
// close the device
device.Close();
}
UnsubscribeFromDeviceEvents(device);
}
throw;
}
}
/// <summary>
/// Lists the devices.
/// </summary>
/// <param name="deviceManager"></param>
private static void ListDevices(DeviceManager deviceManager)
{
var devices = deviceManager.Devices;
for (int i = 0; i < devices.Count; i++)
{
if (!devices[i].Info.IsWIA)
{
Console.WriteLine($"[{(i + 1).ToString("d2")}] {devices[i].Info.ProductName}");
}
}
}
/// <summary>
/// Allows the user to select a scanner.
/// </summary>
/// <param name="deviceManager"></param>
/// <returns>
/// If a scanner is selected, returns the index of the selected scanner and -1 otherwise.
/// </returns>
private static int GetSelectedScanner(DeviceManager deviceManager)
{
do
{
Console.WriteLine("");
Console.Write("Enter the number of the scanner to select it: ");
var enteredString = Console.ReadLine();
if (int.TryParse(enteredString, out int scannerNumber))
{
scannerNumber--;
if ((scannerNumber >= deviceManager.Devices.Count)
|| (scannerNumber < 0))
{
Console.WriteLine("Invalid number. Try again.");
}
else
{
return scannerNumber;
}
}
else if (string.Compare("x", enteredString, true) == 0)
{
return -1;
}
else
{
Console.WriteLine($"{enteredString} is not a number.");
}
} while (true);
deviceManager.Close();
}
/// <summary>
/// Image is acquired.
/// </summary>
private static void device_ImageAcquired(object sender, ImageAcquiredEventArgs e)
{
var path = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), $"image{DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss")}.bmp");
e.Image.Save(path);
Process.Start(path);
// dispose the acquired image
e.Image.Dispose();
}
/// <summary>
/// Scan is completed.
/// </summary>
private static void device_ScanCompleted(object sender, System.EventArgs e)
{
Console.WriteLine("Scan is competed.");
Console.WriteLine("Press enter to exit");
Console.Read();
}
/// <summary>
/// Scan is canceled.
/// </summary>
private static void device_ScanCanceled(object sender, System.EventArgs e)
{
Console.WriteLine("Scan is canceled.");
Console.WriteLine("Press enter to exit");
Console.Read();
}
/// <summary>
/// Scan is failed.
/// </summary>
private static void device_ScanFailed(object sender, ScanFailedEventArgs e)
{
Console.WriteLine(string.Format("Scan is failed: {0}", e.ErrorString));
Console.WriteLine("Press enter to exit");
Console.Read();
}
/// <summary>
/// Called when the scan is finished.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void device_ScanFinished(object sender, System.EventArgs e)
{
Device device = (Device)sender;
// unsubscribe from device events
UnsubscribeFromDeviceEvents(device);
// if device is not closed
if (device.State != DeviceState.Closed)
{
// close the device
device.Close();
}
}
/// <summary>
/// Subscribes to the device events.
/// </summary>
/// <param name="device"></param>
private static void SubscribeToDeviceEvents(Device device)
{
device.ImageAcquired += new System.EventHandler<ImageAcquiredEventArgs>(device_ImageAcquired);
device.ScanCompleted += new System.EventHandler(device_ScanCompleted);
device.ScanCanceled += new System.EventHandler(device_ScanCanceled);
device.ScanFailed += new System.EventHandler<ScanFailedEventArgs>(device_ScanFailed);
device.ScanFinished += new System.EventHandler(device_ScanFinished);
}
/// <summary>
/// Unsubscribes to the device events.
/// </summary>
/// <param name="device"></param>
private static void UnsubscribeFromDeviceEvents(Device device)
{
device.ImageAcquired -= new System.EventHandler<ImageAcquiredEventArgs>(device_ImageAcquired);
device.ScanCompleted -= new System.EventHandler(device_ScanCompleted);
device.ScanCanceled -= new System.EventHandler(device_ScanCanceled);
device.ScanFailed -= new System.EventHandler<ScanFailedEventArgs>(device_ScanFailed);
device.ScanFinished -= new System.EventHandler(device_ScanFinished);
}
}
Steve