View on GitHub

SARAFF SOFTWARE

All products / Saraff.TwainX.Extensions

API Reference

All extension types are in the Saraff.TwainX.Extensions namespace. TWAIN enums and the core session types come from Saraff.TwainX.

XDsm

Data Source Manager wrapper. Inherits System.ComponentModel.Component.

Member Description
static XDsm Create(bool isShowUI = false, TwLanguage language = TwLanguage.RUSSIAN, TwCountry country = TwCountry.BELARUS) Creates a TwainX, adopts it and returns a manager that owns it
static XDsm Create(TwainX twainx, bool isOwns = false) Adopts an existing session, optionally taking ownership
IEnumerable<XDs> DataSources One XDs wrapper per installed data source
TwainX TwainX The underlying session; opens the DSM on access
void Dispose() Unsubscribes, clears the transfer context, cancels async work and disposes the owned session

XDsm.Create(TwainX) throws ArgumentNullException for null. XDsm.TwainX throws InvalidOperationException when the DSM cannot be opened and ObjectDisposedException after the manager has been disposed.

XDs

A lightweight wrapper for one data source.

Member Description
static XDs Create(XDsm dsm, int index) Creates a wrapper; normally obtained from XDsm.DataSources
XCapability<T> GetCapability<T>(TwCap capability) Creates a typed capability wrapper
Task<TwainXAcquisitionResult> AcquireAsync(TwainXAcquireOptions options = null, Action<XCapabilitySession> configure = null, CancellationToken cancellationToken = default) Runs an asynchronous acquisition
bool IsDefault Whether this is the default source
bool IsTwain2Compatible Whether this source supports TWAIN 2.x
TwainX.Identity Identity Source identity (Manufacturer, Family, Name, Version, ProtocolVersion)
XDsm Dsm The owning manager
TwainX TwainX The underlying session; selects and opens this source

XDs.Create throws ArgumentNullException for a null manager and ArgumentOutOfRangeException for a negative index. XDs.TwainX opens the DSM and the source. It closes the previously selected source when necessary and does not reopen an already open source.

Transfer methods

Method Required callbacks Optional callbacks
NativeTransfer(Action<TwainX.EndXferEventArgs> result, ...) result image info, complete, error
MemoryTransfer(Action<TwainX.SetupMemXferEventArgs> setup, Action<TwainX.MemXferEventArgs> transfer, ...) setup, transfer image info, complete, error
MemFileTransfer(Action<TwainX.SetupMemXferEventArgs> setup, Action<TwainX.MemXferEventArgs> transfer, ...) setup, transfer image info, complete, error
FileTransfer(Action<TwainX.SetupFileXferEventArgs> setup, Action<TwainX.FileXferEventArgs> transfer, ...) setup, transfer image info, complete, error

Each method selects and opens the source, sets ICAP_XFERMECH, stores callbacks in the current transfer context and calls TwainX.Acquire(). A second transfer on the same XDsm throws InvalidOperationException.

XCapability<T>

Typed view of one TwCap.

Member Description
static XCapability<T> Create(XDs ds, TwCap cap) Creates a wrapper; normally use XDs.GetCapability<T>
void Set(T value) Set the current value
void Reset() Reset to the TWAIN default
TwCap Id Capability identifier
IEnumerable<XCapabilityValue<T>> Values Allowed values
TwQC IsSupported Support flags
XDs DataSource Owning source, or null for a session-bound capability

XCapability<T>.Create throws ArgumentNullException for a null source.

XCapabilityValue<T>

One allowed value of a capability.

Member Description
static XCapabilityValue<T> Create(XCapability<T> cap, T value) Creates a value; normally obtained from Values
T Value The typed value
XCapability<T> Capability The owning capability
void Set() Make this the current value
bool IsCurrent Equal to GetCurrentCap
bool IsDefault Equal to GetDefaultCap

XCapabilityExtension

Fluent helpers for XCapability<T>.

Method Description
Set<T>(this XCapability<T> capability, Func<XCapability<T>, T> callback) Compute and set a typed value; returns the capability
Set<T>(this XCapability<T> capability, Func<XCapability<T>, XCapabilityValue<T>> callback) Select an allowed value and set it; returns the capability
source.GetCapability<float>(TwCap.XResolution).Set(_ => 300f).DataSource;
source.GetCapability<float>(TwCap.XResolution).Set(cap => cap.Values.First()).DataSource;

A callback that returns null does not call Set.

XCapabilitySession

Capability access inside the AcquireAsync configure callback.

Member Description
XCapability<T> GetCapability<T>(TwCap capability) Capability bound to the session TwainX
TwQC IsCapSupported(TwCap capability) Support flags for the session source
TwainX TwainX The private session instance

The constructor is internal. Instances are supplied by XDs.AcquireAsync. The object and its capabilities are valid only during the callback.

Core types used by the API

Type Use
TwCap Capability identifier
TwQC Capability support flags
TwSX Transfer mechanism (Native, Memory, MemFile, File)
TwPixelType Pixel type (RGB, Gray, BW, …)
TwCompression Compression
TwUnits Measurement units
TwFF File format
TwLanguage / TwCountry DSM language and country
TwainX.EndXferEventArgs Native transfer result (Stream, Cancel)
TwainX.XferDoneEventArgs GetImageInfo(), GetExtImageInfo(...)
TwainX.SetupMemXferEventArgs Memory setup (ImageInfo, BufferSize)
TwainX.MemXferEventArgs Memory chunk (ImageMemXfer)
TwainX.SetupFileXferEventArgs File setup (FileName setter)
TwainX.FileXferEventArgs File result (ImageFileXfer)
TwainX.AcquireErrorEventArgs Exception (a TwainException)
TwainX.ImageInfo Width, height, resolution, bits, planar flag, pixel type, compression
TwainX.ImageMemXfer ImageData, BytesPerRow, Columns, Rows, XOffset, YOffset, Compression
TwainX.ImageFileXfer FileName, Format
TwainX.Identity Manufacturer, Family, Name, Version, ProtocolVersion
TwainXAcquireOptions Async acquisition options
TwainXAcquisitionResult Async result (Streams, ImageInfos)
TwainXAcquisitionProgress PageNumber, ImageInfo

Exceptions

Exception Thrown by Reason
ArgumentNullException XDsm.Create, XDs.Create, XCapability<T>.Create Required argument is null
ArgumentOutOfRangeException XDs.Create Negative source index
InvalidOperationException XDsm.TwainX The DSM could not be opened
InvalidOperationException transfer helpers, AcquireAsync Another transfer is active
ObjectDisposedException XDsm.TwainX, XDs.AcquireAsync The manager has been disposed
InvalidCastException capability values A TWAIN value cannot be converted to T
OperationCanceledException AcquireAsync Cancellation token or manager disposal
TwainException Saraff.TwainX DSM or data source failure

Version and compatibility

The public API is additive relative to 1.0.2.616: the asynchronous members and the session capability type are new. Existing synchronous members keep their signatures. See Migration.