View on GitHub

SARAFF SOFTWARE

All products

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.TwainX and Saraff.Twain are 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

Everyday use

Migration and support

Features

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):

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:

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.