All Projects → soukoku → Ntwain

soukoku / Ntwain

Licence: mit
A TWAIN lib for dotnet.

Projects that are alternatives of or similar to Ntwain

Dryioc
DryIoc is fast, small, full-featured IoC Container for .NET
Stars: ✭ 555 (+1221.43%)
Mutual labels:  netstandard
Headerpropagation
ASP.NET Core middleware to propagate HTTP headers from the incoming request to the outgoing HTTP Client requests. This is a backport to ASP.NET Core 2.1 (and 2.2) of the ASP.NET Core HeaderPropagation middleware I had recently contributed to the ASP.NET Core project.
Stars: ✭ 23 (-45.24%)
Mutual labels:  netstandard
Azuremobileclient.helpers
Provides a wrapper for the Microsoft.Azure.Mobile.Client library
Stars: ✭ 15 (-64.29%)
Mutual labels:  netstandard
Vs Threading
The Microsoft.VisualStudio.Threading is a xplat library that provides many threading and synchronization primitives used in Visual Studio and other applications.
Stars: ✭ 585 (+1292.86%)
Mutual labels:  netstandard
Buildxl
Microsoft Build Accelerator
Stars: ✭ 676 (+1509.52%)
Mutual labels:  netstandard
Brainf cksharp
A complete and full-featured Brainf_ck IDE/console for Windows 10 (UWP), with a high-performance REPL interpreter
Stars: ✭ 26 (-38.1%)
Mutual labels:  netstandard
Opengl.net
Modern OpenGL bindings for C#.
Stars: ✭ 473 (+1026.19%)
Mutual labels:  netstandard
Krakencore
💱 .NET client for Kraken Bitcoin & cryptocurrency exchange API
Stars: ✭ 37 (-11.9%)
Mutual labels:  netstandard
Epplus
EPPlus 5-Excel spreadsheets for .NET
Stars: ✭ 693 (+1550%)
Mutual labels:  netstandard
System.linq.dynamic.core
The .NET Standard / .NET Core version from the System Linq Dynamic functionality.
Stars: ✭ 864 (+1957.14%)
Mutual labels:  netstandard
Steeltoe
Steeltoe .NET Core Components: CircuitBreaker, Configuration, Connectors, Discovery, Logging, Management, and Security
Stars: ✭ 612 (+1357.14%)
Mutual labels:  netstandard
Xamarinmediamanager
Cross platform Xamarin plugin to play and control Audio and Video
Stars: ✭ 647 (+1440.48%)
Mutual labels:  netstandard
Mathos Parser
A mathematical expression parser and evaluation library.
Stars: ✭ 26 (-38.1%)
Mutual labels:  netstandard
Nlog
NLog - Advanced and Structured Logging for Various .NET Platforms
Stars: ✭ 5,296 (+12509.52%)
Mutual labels:  netstandard
Plaid.net
A .NET standard library for interacting with Plaid's banking APIs.
Stars: ✭ 30 (-28.57%)
Mutual labels:  netstandard
Metadata Extractor Dotnet
Extracts Exif, IPTC, XMP, ICC and other metadata from image, video and audio files
Stars: ✭ 518 (+1133.33%)
Mutual labels:  netstandard
Wpfnewprojectsystemsample
Sample WPF application using the new csproj format (.NET Core project system https://github.com/dotnet/project-system/ ). Targering .NET Framework 4.6.2 or higher.
Stars: ✭ 25 (-40.48%)
Mutual labels:  netstandard
Ant Design Blazor
Enterprise-class UI components based on Ant Design and Blazor.
Stars: ✭ 39 (-7.14%)
Mutual labels:  netstandard
Computesharp
A .NET 5 library to run C# code in parallel on the GPU through DX12 and dynamically generated HLSL compute shaders, with the goal of making GPU computing easy to use for all .NET developers! 🚀
Stars: ✭ 982 (+2238.1%)
Mutual labels:  netstandard
Csla
A home for your business logic in any .NET application.
Stars: ✭ 865 (+1959.52%)
Mutual labels:  netstandard

TWAIN Application-Side Library

Note: This was migrated from https://bitbucket.org/soukoku/ntwain/ for v4 dev.

Info

This is a library created to make working with TWAIN interface possible in dotnet. This project has these features/goals:

  • Targets latest TWAIN version (2.3 as of this writing)
  • Supports all the TWAIN functions in the spec.
  • Can host an internal message loop so there's no need to hook into application UI thread.

The solution contains tester projects in winform, wpf, and even console usage. A nuget package is also available here (NOTE: this doc describes v3. For older version go to Source and choose v2 branch for its doc.)

Using the lib

To properly use this lib you will need to be reasonably familiar with the TWAIN spec and understand how it works in general (especially capability). The TWAIN spec can be downloaded from twain.org.

Except for those that have been abstracted away with .net equivalents, most triplet operations are provided as-is so you will need to know when and how to use them. There are no high-level, scan-a-page-for-me-now functions.

The main class to use is TwainSession. You can either use it directly by subscribing to its events or sub-class it and override the On* methods related to those events. The sample projects contain both usages. Note that an application process should only have one active (opened) TwainSession at any time.

#!c#
// can use the utility method to create appId or make one yourself
var appId = TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetExecutingAssembly());

// new it up and handle events
var session = new TwainSession(appId);

session.TransferReady += ...
session.DataTransferred += ...

// finally open it
session.Open();

TwainSession class provides many events, but these 3 are the most important

  • TransferReady - fired before a transfer occurs. You can cancel the current transfer or all subsequent transfers using the event object.
  • DataTransferred - fired after the transfer has occurred. The data available depends on what you've specified using the TWAIN API before starting the transfer. If using native image transfer, the event arg provides a quick GetNativeImageStream() method to convert the data pointer to a System.IO.Stream for use in .net.
  • TransferError - fired when exceptions are encountered during the transfer phase.

NOTE: do not try to close the source/session in the handler of these 3 events or unpredictable things will happen. Either let the scan run its course or cancel the scan using the flag in the TransferReady event arg.

Once you've setup and opened the session, you can get available sources, pick one to use, and call the Open() method of the data source to start using it as the example below:

#!c#

// choose and open the first source found
// note that TwainSession implements IEnumerable<DataSource> so we can use this extension method.
DataSource myDS = session.FirstOrDefault();
myDS.Open();

At this point you can use all the typical TWAIN triplet API for working with a data source.

#!c#

// All low-level triplet operations are defined through these properties.
// If the operation you want is not available, that most likely means 
// it's not for consumer use or it's been abstracted away with an equivalent API in this lib.
myDS.DGControl...;
myDS.DGImage...;

Additionally, the DataSource class has a Capabilities property with the pre-defined wrappers for capability negotiation. You can use the wrapper properties to see what capabilities or their operations are supported. You also won't have to keep track of what enum type to use for a capability. Most capabilities only require a simple single value to set and the wrapper makes it easy to do that (see example below):

#!c#

// The wrapper has many methods that corresponds to the TWAIN capability triplet msgs like
// GetValues(), GetCurrent(), GetDefault(), SetValue(), etc.
// (see TWAIN spec for reference)


// This example sets pixel type of scanned image to BW and
// IPixelType is the wrapper property on the data source.
// The name of the wrapper property is the same as the CapabilityId enum.
PixelType myValue = PixelType.BlackWhite; 

if (myDS.Capabilities.ICapPixelType.CanSet  &&
    myDS.Capabilities.ICapPixelType.GetValues().Contains(myValue))
{
    myDS.Capabilities.ICapPixelType.SetValue(myValue);
}


When you're ready to get into transfer mode, just call Enable() on the data source object.

#!c#

myDS.Enable(...);

After transfer has completed (you are notified of this with the SourceDisabled event from the session object) and you're done with TWAIN, you can close the source and the session in sequence to clean things up.

#!c#

myDS.Close();
session.Close();

Caveats

At the moment the DataTransferredEventArgs only provides conversion routine to an image stream when using native transfer. If other transfer methods are used you'll have to deal with them yourself.

If you just call session.Open() without passing a message loop hook parameter, an internal message loop will be started behind the scenes. When this happens the session events will be raised from another thread. If you would like things marshalled to the UI thread then set the experimental SynchronizationContext property the one from your UI thread.

#!c#
// set this while in a UI thread
session.SynchronizationContext = SynchronizationContext.Current;

Note that on certain scanner drivers this may hang the application due to their use of modal dialogs, so if you find yourself in that position you'll have to find another way to synchronize data to UI threads.

Using the new twaindsm.dll

By default NTwain will use the newer data source manager (twaindsm.dll) if available. To override this behavior set the PlatformInfo's PreferNewDSM flag to false. This is necessary due to some older sources not working with the newer dsm.

#!c#
// go back to using twain_32.dll under windows,
// do this once at app startup.
NTwain.PlatformInfo.Current.PreferNewDSM = false;

If the application process is going to be running in 64-bit then this flag will have no effect and you will always need to have the twaindsm installed.

If the scanner's TWAIN driver is still 32-bit then you'll have need to compile the application exe in x86 or you won't see the driver.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].