Saraff.Twain.DS.BitmapSource 2.1.6

dotnet add package Saraff.Twain.DS.BitmapSource --version 2.1.6
                    
NuGet\Install-Package Saraff.Twain.DS.BitmapSource -Version 2.1.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Saraff.Twain.DS.BitmapSource" Version="2.1.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Saraff.Twain.DS.BitmapSource" Version="2.1.6" />
                    
Directory.Packages.props
<PackageReference Include="Saraff.Twain.DS.BitmapSource" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Saraff.Twain.DS.BitmapSource --version 2.1.6
                    
#r "nuget: Saraff.Twain.DS.BitmapSource, 2.1.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Saraff.Twain.DS.BitmapSource@2.1.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Saraff.Twain.DS.BitmapSource&version=2.1.6
                    
Install as a Cake Addin
#tool nuget:?package=Saraff.Twain.DS.BitmapSource&version=2.1.6
                    
Install as a Cake Tool

Saraff.Twain.DS.BitmapSource

Simplifies the creation of a TWAIN Data Source that returns a bitmap.

license: LGPL 3.0 or later build NuGet version NuGet downloads

Installation

PM> Install-Package Saraff.Twain.DS.BitmapSource

Compatibility

Target Supported
net48 Yes
net8.0-windows Yes
net5.0-windows No, use net8.0-windows or net48

The package depends on Saraff.Twain.DS 1.2.0. Version 2.0.0 removes the net5.0-windows target and changes the assembly version to 2.0.0.0; rebuild consumers against the new package. Version 2.1.0 adds DAT_PALETTE8 support for 1, 4 and 8 bits per pixel.

Usage

Derive from BitmapDataSource and implement Acquire:

using System.Drawing;
using Saraff.Twain.DS.BitmapSource;

public sealed class SampleDataSource : BitmapDataSource {
    protected override Bitmap Acquire() {
        return new Bitmap("page.png");
    }
}

A production Data Source also needs the usual TWAIN entry point and capability attributes provided by Saraff.Twain.DS. See the Saraff.Twain.DS wiki for a complete example.

To enable palette transfer, declare TwPixelType.Palette on the concrete Data Source and expose bit depths 1, 4 and 8 through ICAP_BITDEPTH. PixelTypeAttribute is not inherited, so the attribute must be applied to the concrete class:

[PixelType(TwPixelType.RGB, TwPixelType.Gray, TwPixelType.Palette)]
public sealed class SampleDataSource : BitmapDataSource {
    public SampleDataSource() {
        this[TwCap.BitDepth].Value=new ushort[] { 1,4,8,24 };
        this[TwCap.BitDepth].Value=(DefaultValue<ushort>)8;
    }
}

For 1-bit palette transfer, ICAP_BITORDER defaults to TWBO_MSBFIRST. To allow TWBO_LSBFIRST, add [LsbFirstSupported] to the concrete Data Source; the Source reverses the bits of each row during memory transfer.

Transfer behavior

  • Native and memory transfers support 24bppRgb, 48bppRgb, identity-grayscale 8bppIndexed and palette 1bppIndexed, 4bppIndexed, 8bppIndexed.
  • A grayscale request is converted to 8bpp grayscale; a palette request uses the requested bit depth (1, 4 or 8), preserving the source depth when ICAP_BITDEPTH is not a palette depth; other formats are converted to 24bppRgb.
  • Memory transfer rows are padded with zero bytes to a 32-bit boundary as required by TWAIN 2.4; BytesPerRow reports the padded row size, and palette images are transferred as packed indices within each row. The palette is reported by DG_IMAGE / DAT_PALETTE8 for MSG_GET and MSG_GETDEFAULT; MSG_SET and MSG_RESET return TWRC_FAILURE / TWCC_BADPROTOCOL.
  • TW_PALETTE8.NumColors is 2, 16 or 256 depending on the palette bit depth.
  • MSG_GET returns the palette of the last acquired image and falls back to a depth-appropriate default palette before the first acquisition.
  • Native BMP transfers and file transfers embed the palette in the image format itself; ICAP_BITORDER applies only to 1-bit memory transfers.
  • Bilevel requests are transferred as 24bppRgb.
  • File transfer supports BMP, JPEG, PNG and TIFF. EXIF and multi-page TIFF are not supported.
  • The memory transfer buffer is raised automatically when one image row does not fit into the default buffer.
  • ICAP_XFERCOUNT = -1 is supported. The indeterminate series ends when Acquire() returns null after a transfer; the source calls EndOfXfers() and returns to state 5.
  • Image layout (crop/scale) and compression are not applied; the acquired bitmap is transferred as a whole and uncompressed.
  • The image is released on end-of-transfer, reset and before the next acquisition.

Build

dotnet build Saraff.Twain.DS.BitmapSource.sln -c Release
dotnet test Saraff.Twain.DS.BitmapSource.sln -c Release
dotnet pack Saraff.Twain.DS.BitmapSource.csproj -c Release -o artifacts
Product Compatible and additional computed target framework versions.
.NET net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net10.0-windows was computed. 
.NET Framework net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.6 54 11/30/2026
2.1.3 60 11/30/2026
2.1.2 62 11/29/2026
2.1.1 65 11/29/2026
2.1.0 70 11/29/2026
1.0.5-rc755 475 1/5/2023
1.0.4.546 1,384 12/25/2019
1.0.3.546 833 10/22/2019
1.0.2.546 874 6/21/2019
1.0.1.546 3,013 9/21/2016