View on GitHub

SARAFF SOFTWARE

All products

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 in Saraff.TwainX.TwainX. The extensions project only makes the session and capability API more convenient.

Saraff.TwainX.Extensions is not interchangeable with Saraff.Twain.Extensions. The two packages use different core assemblies (Saraff.TwainX vs Saraff.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

Everyday use

Migration and maintainers

Requirements

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