Saraff.TwainX
Saraff.TwainX is a fully managed, stream-based TWAIN 2.x component for .NET Standard. It is the cross-platform counterpart of Saraff.Twain.NET.
| Package | Saraff.TwainX |
| Main class | Saraff.TwainX.TwainX |
| Target framework | netstandard2.0 |
| TWAIN | 2.x / DS2 data sources only |
| Images | Stream (no System.Drawing.Image dependency) |
| UI | No WinForms/WPF dependency; Parent is IntPtr |
| Async | TwainXAcquisitionSession.AcquireAsync() |
| License | LGPL-3.0-or-later |
Important:
Saraff.TwainXandSaraff.Twainare different products. They have different package ids, assembly names, namespaces and image models. Do not mix their types.
The model in one picture
flowchart LR
subgraph App["Application"]
UI["UI / service / console"]
SYNC["Synchronous Acquire()"]
ASYNC["AcquireAsync()"]
end
subgraph X["Saraff.TwainX"]
T["TwainX component"]
THR["Dedicated session thread<br/>STA + Win32 pump on Windows"]
SINK["Stream capture"]
end
subgraph TWAIN["TWAIN 2.x"]
DSM["TWAINDSM"]
DS["DS2 data source"]
end
UI --> SYNC --> T
UI --> ASYNC --> THR --> T
T --> DSM --> DS
T --> SINK --> UI
TwainX opens the DSM and the source, enables the source and raises events when image data arrives.
The synchronous API uses the caller’s thread; the asynchronous API creates a dedicated session
thread and returns a result object.
Quick start: synchronous
using Saraff.TwainX;
IntPtr parentHandle = GetMainWindowHandle();
using (var twain = new TwainX()) {
twain.ShowUI = true;
twain.DisableAfterAcquire = true;
twain.Parent = parentHandle;
twain.EndXfer += (sender, e) => {
using (var stream = e.Stream) {
// Copy or save the image before the session is closed.
using (var file = File.Create($"scan-{Guid.NewGuid():N}.png")) {
stream.CopyTo(file);
}
}
};
twain.AcquireError += (sender, e) => {
Console.WriteLine(e.Exception.Message);
};
twain.Acquire();
}
Acquire() requires a TWAIN 2.x/DS2 source and a TWAIN 2.x DSM.
Quick start: asynchronous
using Saraff.TwainX;
using (var session = new TwainXAcquisitionSession(new TwainXAcquireOptions {
ShowUI = true,
DisableAfterAcquire = true,
MaxImages = 1
})) {
using (var result = await session.AcquireAsync()) {
foreach (var stream in result.Streams) {
using (var file = File.Create($"scan-{Guid.NewGuid():N}.png")) {
stream.CopyTo(file);
}
}
}
}
The result owns its streams; they are copies and remain valid after the TWAIN session is closed.
Use the AcquireAsync(twain => { ... }) overload to set capabilities or image layout before the
source is enabled.
See Async Acquisition for completion rules, cancellation and platform details.
Install:
dotnet add package Saraff.TwainX
Documentation map
Start here
- Getting Started — sync and async first scan
- Async Acquisition — dedicated STA thread,
MaxImages, cancellation - Architecture — components, session thread and event flow
- Samples - minimal, advanced and DI examples for 1.0.34
Everyday use
- Threading and UI —
Acquire()vsAcquireAsync(),Parent, event threads - Images and Transfer — streams, ownership, transfer modes, progress
- Image Pipeline —
IStreamProvider,IImageHandler,IImageFactory<T> - API Reference — public types at a glance
Migration and support
Features
- TWAIN 2.x / DS2 only
netstandard2.0; usable from .NET Framework 4.6.1+, .NET Core/.NET 5+ and Mono (a native TWAIN DSM is required for the target platform)- No WinForms, WPF or
System.Drawing.Commondependency - Native, Buffered Memory, Disk File and Memory File transfer modes
- Stream-based image access with explicit ownership
- Source enumeration and selection
- ADF and duplex control through TWAIN capabilities
- Extended image information
- Synchronous
Acquire()and asynchronousAcquireAsync() - Optional
AcquireAsyncconfigure callback for capabilities and image layout - Dedicated STA thread with a Win32 message pump on Windows for async acquisition
Supported platforms
| Platform | Synchronous Acquire() |
Asynchronous AcquireAsync() |
|---|---|---|
| Windows | Yes, on an application message-loop thread | Yes, dedicated STA thread + Win32 pump |
| Linux | Yes, depending on DSM | Dedicated thread without Win32 pump; best effort |
| macOS | Yes, depending on DSM | Dedicated thread without Win32 pump; best effort |
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.
Samples
Current examples for Saraff.TwainX 1.0.34 (source and published binaries):
- Minimal CLI
- Minimal WinForms
- Minimal WPF
- Advanced CLI
- Advanced WinForms
- Advanced WPF
- DI CLI
- DI WinForms
- DI WPF
See Samples for the feature matrix, run commands and the
net48 platform note.
Legacy archives
The 2018 archives target older package names; the API shape is still useful:
- Saraff.TwainX.NET Core Sample1
- Saraff.TwainX.NET Core Sample2
- Saraff.TwainX.NET Core Sample3
- Saraff.TwainX.NET WinForms Sample1
- Saraff.TwainX.NET WinForms Sample2
- Saraff.TwainX.NET WPF Sample1
Legacy screenshots

Figure 1 - Saraff.TwainX Core Sample2 on Windows 10

Figure 2 - Saraff.TwainX WinForms Sample2 on Windows 10

Figure 3 - Saraff.TwainX WPF Sample1 on Windows 10
If you notice an error, please let me know about it.