Features

A few examples on how to get started with TWAINScanning.NET. We will try to cover the most common use cases of the library

list of the following feature

Minimal example using scanners UI

This will launch the scanners user interface where you can select the desired settings. All scanned pages will be saved to separate JPEGs.

  • using (var dsm = new DataSourceManager(this))	// opens TWAIN
  • 	using (var ds = dsm.OpenSource())	// opens default source
  • 	{
  • 		var collector = ds.Acquire(true);	// acquires images 
  • 		collector.SaveAllToJpegs(@"C:\SomeFolder\Some\name.jpeg");	// saves images to disk 
  • 	}

Without showing the scanners interface scan 20 pages from ADF and save it to a PDF

Here we will scan using predefined settings. In this case, we will open the default scanning device, use an A4 page size, set the resolution to 300 DPI and then scan 20 pages from the ADF in black and white. The scanned pages will be then saved to a single PDF document.

  • using (var dsm = new DataSourceManager(this))	// opens TWAIN
  • 	using (var ds = dsm.OpenSource())	// opens default source
  • 	{
  • 		ds.PageSize.Value = TwSS.A4;	// sets page size to A4
  • 		ds.Resolution.Value = 300f;	// sets 300 DPI
  • 		ds.ColorMode.Value = TwPixelType.BW;	// sets Black and white format
  • 		ds.UseFeeder.Value = true;	// scanner will ty to use document feeder if possible
  • 		ds.UseDuplex.Value = true;	// scanner will ty to use duplex if possible
  • 		
  • 		var collector = ds.Acquire(false, true, TwSX.Native, 20);	// acquires images 
  • 		
  • 		collector.SaveAllToMultipagePdf(@"C:\SomeFolder\Some\name.pdf");	// saves images to disk 
  • 	}

Iterate over installed scanners on the system and get their information

This code example lists all the installed scanning devices on the host. It will list out the scanners name and allow you to select the default device for future scans.

  • using (var dsm = new DataSourceManager(this))// opens TWAIN
  • {
  • 	// listing and write scanner names
  • 	List<TwIdentity> scannerIds = dsm.AvailableSources();
  • 	foreach (TwIdentity id in scannerIds)
  • 		Console.WriteLine(id.ProductName);
  • 
                            
  • 	string defaultScannerName = dsm.DefaultSource().ProductName;	// getting default source name
  • 
                            
  • 	dsm.SelectDefaultSourceDlg();	// Setting the default scanner by default twain dialog
  • }

Get basic details about a scanner.

In this sample, we will open the default scanning device and list out the basic settings of the device like available page sizes, resolutions, color modes, etc.

  • using (var dsm = new DataSourceManager(this))	// opens TWAIN
  • 	using (var ds = dsm.OpenSource())	// opens default source
  • 	{
  • 		TwSS[] pageSizes = ds.PageSize.AvailableValues;
  • 		float[] resolutions = ds.Resolution.AvailableValues;
  • 		TwPixelType[] colorModes = ds.ColorMode.AvailableValues;
  • 		bool[] adfEnabledStates = ds.UseFeeder.AvailableValues;
  • 		bool[] duplexEnabledStates = ds.UseDuplex.AvailableValues;
  • 	}

Look at more advanced settings of a scanning device

Here we illustrate on how to retrieve more advanced information about a scanner. More advanced settings will be stored in the Datasource.Settings object.

  • using (var dsm = new DataSourceManager(this))	// opens TWAIN
  • 	using (var ds = dsm.OpenSource())	// opens default source
  • 	{
  • 		Console.Write("Battery percentage: ");
  • 		var capBattPerc = ds.Settings.PowerMonitoring.BatteryPercentage;
  • 		if (capBattPerc.IsSupportedOnThisDevice())	// scanner can provide information about battery percentage
  • 		{
  • 			TwBM2 perc = capBattPerc.Value;
  • 			if (perc == TwBM2.Infinite)
  • 				Console.WriteLine("INFINITE POWER!");
  • 			else if (perc == TwBM2.CannotReport)
  • 				Console.WriteLine("Don't know.");
  • 			else
  • 				Console.WriteLine("Battery at " + (short)perc + "%");
  • 		}
  • 		else
  • 		{
  • 			Console.WriteLine("Don't have battery!");
  • 		}
  • 
                            
  • 		Console.Write("Orientation: ");
  • 		var capOrientation = ds.Settings.ImageAcquire.Orientation;
  • 		if (capOrientation.IsSupportedOnThisDevice())	// scanner can provide information about battery percentage
  • 		{
  • 			TwOR orientation = capOrientation.Value;
  • 			Console.Write(" Current orientation is " + orientation);
  • 			
  • 			Console.Write(" All possible orientations are:");
  • 			foreach(TwOR orient in capOrientation.SupportedValues)
  • 				Console.Write(" " + orient);
  • 		}
  • 		else
  • 		{
  • 			Console.WriteLine("Scanner doesn't support orientation");
  • 		}
  • 	}

Scan asynchronously

In case you would like to scan asynchronously use the sample below. Scanning this way allows you to perform the scanning job in the background and allows your users to continue working in the app.

  • public partial class ScanningForm : Form
  • {
  • 	DataSourceManager dsm = null;
  • 	DataSource ds = null;
  • 	public ScanningForm()
  • 	{
  • 		dsm = new DataSourceManager(this);
  • 		InitializeComponent();
  • 	}
  • 	
  • 	private void buttonScann_Click(object sender, EventArgs e)
  • 	{
  • 		dsm.SelectDefaultSourceDlg();	// sets default scanner
  • 		ds = dsm.OpenSource();	// opens default scanner
  • 		ds.AcquireAsync(onFinishedScanning, true, true, TwSX.Memory, -1);
  • 	}
  • 
                            
  • 	private void onFinishedScanning(TwImgCollector collector)
  • 	{
  • 		collector.SaveAllToMultipageTiff(@"C:\someFolder\someFile.tiff");	// saving images
  • 		collector.Dispose();	// disposing collector
  • 		ds.Dispose();	// disposing scanner
  • 		ds = null;
  • 	}
  • }

Try TwainScanning.NET demo for free

This website uses cookies

This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy.
Read more