Using a Twain Scanner from C#

We have an example project for C# that shows how to use the csXImage ActiveX control to acquire an image from a Twain enabled scanner. Download the project code.

The demo uses the trial version of csXImage, which must must be registered on the development computer and the licence file must be in the same folder as the .ocx control. Running our installer will have performed the registration and unpacking of files.

When the C# form is loaded, and event runs to populate the combo boxes with the available Twain devices and pixel types. TwainDeviceCount returns the number of Twain devices installed on the system. TwainDeviceName returns the name of each device and the index of the device will be used later to set the CurrentTwainDevice property. This is an alternative approach to setting the device with the SelectTwainDevice method which uses a standard dialogue box from the Twain DLL to select the scanner.

private void fMain_Load(object sender, EventArgs e)
{
  int n;

  // Initialise the contents of the various comboboxes
  n = axImageBox1.TwainDeviceCount;
  cboSelect.Items.Clear();
  for (int i = 1; i <= n; i++)
    cboSelect.Items.Add(axImageBox1.get_TwainDeviceName(i - 1));
  cboSelect.SelectedIndex = 0;

  cboPixType.Items.Clear();
  cboPixType.Items.Add("Black/White");
  cboPixType.Items.Add("Greyscale");
  cboPixType.Items.Add("RGB Colour");
  cboPixType.SelectedIndex = 2;

  cboFileExt.Items.Clear();
  cboFileExt.Items.Add(".bmp");
  cboFileExt.Items.Add(".gif");
  cboFileExt.Items.Add(".jpg");
  cboFileExt.Items.Add(".pcx");
  cboFileExt.Items.Add(".png");
  cboFileExt.Items.Add("psd");
  cboFileExt.Items.Add(".pdf");
  cboFileExt.SelectedIndex = 0;
}

Here is a screenshot of the application after the form has loaded. One or more devices have been found and the first is shown as an Epson scanner.

ActiveX Twain example for C#

If the check box is ticked, the scanner's own interface will be used. The user can then set the page size and resolution using this interface. Alternatively, the box can be cleared and the scanner settings will be sent from csXImage. The middle part of the form allows the user to set the pixel type, resolution and page layout.

The code to acquire the image is quite simple. In this example, most of the code is involved with reading the form to set scanner properties if the default interface is not used. The important code for scanning is to set CurrentTwainDevice to select the scanner. Then call Acquire to scan or load the default interface. Once the scan is complete the image will be loaded into the csXImage control where further processing can take place or the image can be saved.

private void cmdAcquire_Click(object sender, EventArgs e)
{
  double resolution;
  double tlLeft, tlRight, tlTop, tlBottom;

  // Select the device shown in the combobox
  axImageBox1.CurrentTwainDevice = cboSelect.SelectedIndex;

  // Only continue if the device is connected
  if (axImageBox1.TwainConnected)
  {
    // Either use the interface, or set properties from the
    // various text boxes and the combo box
    if (chkUseInt.Checked)
      axImageBox1.UseTwainInterface = true;
    else
    {
      axImageBox1.TwainPixelType =
        (csXImageTrial.TxPixelType)cboPixType.SelectedIndex;
      if (Double.TryParse(txtRes.Text, out resolution))
        axImageBox1.TwainResolution = resolution;
      if ((Double.TryParse(txtTop.Text, out tlTop)) &&
          (Double.TryParse(txtLeft.Text, out tlLeft)) &&
          (Double.TryParse(txtRight.Text, out tlRight)) &&
          (Double.TryParse(txtBtm.Text, out tlBottom)))
        axImageBox1.SetTwainLayout(tlLeft, tlRight, tlTop, tlBottom);
      axImageBox1.UseTwainInterface = false;
    }
  }
  // Acquire the image
  axImageBox1.Acquire();
}

Cookies

This site uses cookies for functionality, traffic analysis and for targeted advertising. Click the Accept button to accept our Cookie Policy. The Cookie Policy page offers configuration for a reduced set of cookies for this site.