All products / Saraff.TwainX.Extensions
Capabilities
TWAIN capabilities are addressed by a TwCap identifier. The extensions add the value type:
var resolution = source.GetCapability<float>(TwCap.XResolution);
var pixelType = source.GetCapability<TwPixelType>(TwCap.IPixelType);
var fileFormat = source.GetCapability<TwFF>(TwCap.ImageFileFormat);
var units = source.GetCapability<TwUnits>(TwCap.IUnits);
GetCapability<T> only creates a wrapper. The first operation that needs the data source calls
XDs.TwainX, which selects and opens the source. See Architecture.
XCapability<T>
| Member | Description |
|---|---|
TwCap Id |
Capability identifier |
void Set(T value) |
Set the current value |
void Reset() |
Reset to the TWAIN default |
IEnumerable<XCapabilityValue<T>> Values |
Allowed values |
TwQC IsSupported |
Support flags |
XDs DataSource |
Owning source, or null for a session-bound capability |
resolution.Set(200f);
resolution.Set(300f);
var current = resolution.Values.FirstOrDefault(v => v.IsCurrent);
Console.WriteLine($"Current: {current?.Value}");
The wrapper is not validated when it is created. A capability may be unsupported by a particular
source, in which case the underlying TwainX call fails. Query IsSupported when the capability is
optional:
if ((resolution.IsSupported & TwQC.Set) != 0)
{
resolution.Set(300f);
}
TwQC is a flags enum. Check individual operations (Get, Set, GetCurrent, GetDefault,
Reset) instead of comparing the whole value when the application only needs one operation.
XCapabilityValue<T>
Values converts the TWAIN representation (Range, array, single value or string) into typed
values:
| Member | Description |
|---|---|
T Value |
The typed value |
XCapability<T> Capability |
Owning capability |
void Set() |
Make this value current |
bool IsCurrent |
Equal to GetCurrentCap |
bool IsDefault |
Equal to GetDefaultCap |
foreach (var value in pixelType.Values)
{
Console.WriteLine($"{value.Value} current={value.IsCurrent} default={value.IsDefault}");
}
pixelType.Values.First(v => v.IsDefault).Set();
IsCurrent and IsDefault call the data source each time they are evaluated; they are not cached.
Fluent helpers
XCapabilityExtension adds Set overloads that accept a callback and return the capability, so
capability chains can continue through DataSource:
using System.Linq;
source
.GetCapability<float>(TwCap.XResolution).Set(_ => 300f).DataSource
.GetCapability<float>(TwCap.YResolution).Set(_ => 300f).DataSource
.GetCapability<TwPixelType>(TwCap.IPixelType).Set(_ => TwPixelType.RGB).DataSource
.GetCapability<TwUnits>(TwCap.IUnits).Set(_ => TwUnits.Inches).DataSource;
Two overloads are available:
XCapability<T> Set<T>(this XCapability<T> capability, Func<XCapability<T>, T> callback);
XCapability<T> Set<T>(this XCapability<T> capability, Func<XCapability<T>, XCapabilityValue<T>> callback);
A callback that returns null does not call Set; the capability is returned unchanged. Use this
with FirstOrDefault when a value is optional:
source.GetCapability<float>(TwCap.XResolution)
.Set(cap => cap.Values.FirstOrDefault(v => v.Value >= 300f));
The instance method Set(T) returns void, so use the callback overload when chaining.
Value conversion
Capability values are converted by the internal ValueConverter:
| TWAIN value | Requested T |
Result |
|---|---|---|
int, long, short |
int, float, double, string, … |
Convert.ChangeType with invariant culture |
| numeric or string | enum | Enum.ToObject / Enum.Parse |
| another enum | enum | converted through the underlying integral type |
| array of any element type | T |
each element converted independently |
Range |
T |
each value produced by FromRange is converted |
null |
reference type or Nullable<T> |
null / default |
null |
non-nullable value type | InvalidCastException |
| unsupported object | any | InvalidCastException |
Examples:
var units = source.GetCapability<TwUnits>(TwCap.IUnits); // enum from int
var timestamp = source.GetCapability<string>(TwCap.DeviceTimeDate); // TW_STR32
var number = source.GetCapability<int>(TwCap.XResolution); // float -> int, truncates
Choose T to match the capability type. Using int for a floating-point resolution truncates the
value; using an incompatible reference type throws InvalidCastException.
Session-bound capabilities
Inside XDs.AcquireAsync, the configure callback receives an XCapabilitySession:
await source.AcquireAsync(
options,
session =>
{
session.GetCapability<float>(TwCap.XResolution).Set(300f);
session.GetCapability<float>(TwCap.YResolution).Set(300f);
session.GetCapability<TwPixelType>(TwCap.IPixelType).Set(TwPixelType.RGB);
});
XCapabilitySession exposes:
| 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 callback runs on the acquisition thread after the source is opened and before Acquire(). The
returned capabilities and the session TwainX are valid only during that callback. For
session-bound capabilities, XCapability<T>.DataSource is null; do not use XDs.TwainX inside
the callback.
XCapabilitySession capabilities use the same XCapability<T> and XCapabilityValue<T> classes as
the synchronous API, including the conversion rules above.