Saraff.TwainX.Extensions
Saraff.TwainX.Extensions is a small, strongly typed, LINQ-style layer on top of
Saraff.TwainX. It wraps the TWAIN Data Source Manager and data sources
in XDsm / XDs, exposes capabilities as XCapability<T>, and represents capability values as
XCapabilityValue<T> objects that can be inspected and selected.
| Package | Saraff.TwainX.Extensions |
| Namespace | Saraff.TwainX.Extensions |
| Target framework | netstandard2.0 |
| Depends on | Saraff.TwainX 1.0.35 |
| License | LGPL-3.0-or-later |
| Current version | 1.0.3.616 |
| Repository | Saraff.Twain.Extensions |
This package is an add-on. It does not replace
Saraff.TwainX: the TWAIN session, stream capture and asynchronous acquisition live inSaraff.TwainX.TwainX. The extensions project only makes the session and capability API more convenient.
Saraff.TwainX.Extensionsis not interchangeable withSaraff.Twain.Extensions. The two packages use different core assemblies (Saraff.TwainXvsSaraff.Twain), different namespaces and different image models.
What it adds
| Type | Purpose |
|---|---|
XDsm |
Data Source Manager wrapper; enumerates sources, owns the TwainX session |
XDs |
A single data source; identity, default/TWAIN 2 checks, capability and transfer helpers |
XCapability<T> |
Strongly typed capability: Set, Reset, Values, IsSupported |
XCapabilityValue<T> |
One allowed capability value: Value, Set(), IsCurrent, IsDefault |
XCapabilityExtension |
Fluent Set(...) helpers for capability chains |
XCapabilitySession |
Capability access inside the asynchronous AcquireAsync configure callback |
Quick start: synchronous transfer
using System;
using System.IO;
using System.Linq;
using Saraff.TwainX;
using Saraff.TwainX.Extensions;
using (var dsm = XDsm.Create(isShowUI: true))
{
var source = dsm.DataSources.FirstOrDefault();
if (source == null)
{
Console.WriteLine("No TWAIN data sources found.");
return;
}
source
.GetCapability<float>(TwCap.XResolution).Set(_ => 300f).DataSource
.GetCapability<float>(TwCap.YResolution).Set(_ => 300f).DataSource
.GetCapability<TwPixelType>(TwCap.IPixelType).Set(_ => TwPixelType.RGB).DataSource
.NativeTransfer(
e =>
{
using (var stream = e.Stream)
using (var file = File.Create("scan.png"))
{
stream.CopyTo(file);
}
},
errorCallback: e => Console.Error.WriteLine(e.Exception));
}
NativeTransfer selects and opens the data source, sets ICAP_XFERMECH to SX_NATIVE, starts
acquisition and delivers each image through the EndXfer callback. XDsm.Dispose() clears the
active transfer context, unsubscribes from TwainX events and disposes the owned session.
In a WinForms or WPF application, keep XDsm alive for the form lifetime. Do not wrap it in a
short using block while asynchronous TWAIN callbacks may still arrive.
Quick start: asynchronous transfer
using System;
using System.IO;
using System.Linq;
using Saraff.TwainX;
using Saraff.TwainX.Extensions;
using (var dsm = XDsm.Create())
{
var source = dsm.DataSources.First();
var options = new TwainXAcquireOptions
{
ShowUI = false,
MaxImages = 1,
Progress = new Progress<TwainXAcquisitionProgress>(p =>
Console.WriteLine($"Page {p.PageNumber}: {p.ImageInfo.ImageWidth}x{p.ImageInfo.ImageLength}"))
};
using (var result = await source.AcquireAsync(
options,
session => session.GetCapability<TwPixelType>(TwCap.IPixelType).Set(TwPixelType.RGB)))
{
for (var i = 0; i < result.Streams.Count; i++)
{
using (var file = File.Create($"scan-{i}.png"))
{
result.Streams[i].CopyTo(file);
}
}
}
}
AcquireAsync runs the acquisition on a dedicated session thread created by
Saraff.TwainX, returns all pages as result-owned MemoryStream copies, supports cancellation and
progress, and uses native transfer internally. The caller owns the result and must dispose it.
Install:
dotnet add package Saraff.TwainX.Extensions --version 1.0.3.616
Documentation map
Start here
- Getting Started - install, enumerate sources, configure and scan
- Architecture - layers, ownership, lifecycle and concurrency
- Capabilities - typed values, support flags, current/default values
Everyday use
- Image transfers - native, memory, memory-file and file modes
- Asynchronous acquisition - options, source selection, progress, cancellation
- Samples - console, WinForms and async scenarios
- API Reference - public types at a glance
- Troubleshooting - DSM, DS2, bitness, threading and capability errors
Migration and maintainers
- Migration from 1.0.2.616 - dependency, async API and behavior changes
- Building and publishing - build, test, pack and NuGet Trusted Publishing
Requirements
- A TWAIN 2.x Data Source Manager and at least one DS2 data source
- A .NET implementation that supports
netstandard2.0(.NET Framework 4.6.1+, .NET Core, .NET 5+) - A process bitness that matches the installed DSM and data source (x86 or x64)
Windows is the primary supported platform. Linux and macOS support depends on the availability of a
compatible DSM and source. Saraff.TwainX requires a TWAIN 2.x DSM and a DS2 source; TWAIN 1.x
sources are supported only by the Windows Saraff.Twain product. See
Saraff.TwainX for the platform matrix and the core session API.
Features
- Typed
XDsm/XDs/XCapability<T>model overSaraff.TwainX - Capability values with
IsCurrent/IsDefaultchecks and fluentSet(...)helpers - Native, buffered memory, memory-file and disk-file transfer helpers
- Per-transfer callback isolation and protection against overlapping transfers
- Asynchronous acquisition through
XDs.AcquireAsync - Progress reporting and cooperative cancellation
- Explicit event unsubscription and session disposal
netstandard2.0; no WinForms, WPF orSystem.Drawing.Commondependency